变量'ddl'在赋值之前使用 [英] variable 'ddl' is used before it has been assigned a value

查看:105
本文介绍了变量'ddl'在赋值之前使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人可以告诉我如何或者我需要分配给varicale'ddl',因为我在获取此错误后变得有点卡住'变量'ddl'在被赋值之前被使用'



我试图使用查询字符串从数据库填充我的下拉列表。



 如果 Request.QueryString(  userid)<>   然后 

Dim ddlResult As DataSet
ddlResult = GetUserName.ddlData(Request.QueryString(< span class =code-string> userid)。ToString)

Dim ddl As DropDownList

对于 每次遇到 ddlResult.Tables( 0 )。行

Dim ddlString As String = encounter.Item( description)。ToString

Dim newListItem As ListItem
newListItem = ListItem( description
ddl.Items.Add(newListItem)

下一步

结束 如果



谢谢



我尝试添加更多代码,如下所示,但DDL没有出现在这页纸。我不能直接将DDL添加到页面,因为由于某种原因它停止按钮工作我用来打开页面。



 如果 Request.QueryString(  userid )<>   然后 

Dim ddl As DropDownList = DropDownList

Dim ddlResult As DataSet
ddlResult = GetUserName。 ddlData(Request.QueryString( userid)。ToString)

< span class =code-keyword> Dim dv As HtmlGenericControl( div
dv.ID = sysData

对于 每次遇到 ddlResult.Tables( 0 )。行

Dim ddlString 作为 字符串 = encounter.Item( description description)。ToString

Dim newListItem As ListItem
newListItem = ListItem( description description


ddl.Items.Add( newListItem)
dv.Controls.Add(ddl)
sysData.Controls.Add(dv)

Next

结束 如果

解决方案

< blockquote>实例化ddl

 Dim ddl As New DropDownList 


你所做的就是创建一个变量 DDL 。您需要先为该变量指定一个值,然后才能向其中添加项目。

例如

  Dim  ddl  As  DropDownList = this.Page.Master.FindControl(  YourDropDownList



如果这是WinForm,那么

  Dim  ddl 作为 DropDownList = this.YourDropDownList 



(你需要替换YourDropDownList的页面/表单上的下拉列表的ID,或者您可以使用

  Dim  ddl 作为 DropDownList =  DropDownList()

- 但是没有人会看到你的列表


更改

  Dim  ddl 作为 DropDownList 



to

  Dim  ddl 正如  DropDownList 



使用New关键字创建DropDownList的新实例



如果您已将DropDownList添加到您的aspx页面,如下所示

  <   asp:DropDownList     ID   =  DropDownList1    runat   =  server    /  >  



那么你不需要创建新的。通过ID访问它,示例代码

 对于 每个遇见 ddlResult.Tables( 0 )。行

< span class =code-keyword> Dim ddlString As String = encounter.Item( description)。ToString

Dim newListItem As ListItem
newListItem = ListItem( description
DropDownList1.Items.Add(newListItem)

< span class =code-keyword>下一步


Hi All,

Can someone please advise me how or what i need to assign to the varicale 'ddl' as i a little stuck after getting this error 'variable 'ddl' is used before it has been assigned a value'

I am trying to populate my dropdown with data from database using the querystring.

If Request.QueryString("userid") <> "" Then

         Dim ddlResult As DataSet
         ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

         Dim ddl As DropDownList

         For Each encounter In ddlResult.Tables(0).Rows

             Dim ddlString As String = encounter.Item("description").ToString

             Dim newListItem As ListItem
             newListItem = New ListItem("description")
             ddl.Items.Add(newListItem)

         Next

     End If


thank you

I have tried adding a little bit more code as shown below but the DDL is not showing up on the page. I cant add the DDL direct to the page because for some reason it stops the button from working i use to open the page.

If Request.QueryString("userid") <> "" Then

          Dim ddl As DropDownList = New DropDownList

          Dim ddlResult As DataSet
          ddlResult = GetUserName.ddlData(Request.QueryString("userid").ToString)

          Dim dv As New HtmlGenericControl("div")
          dv.ID = "sysData"

          For Each encounter In ddlResult.Tables(0).Rows

              Dim ddlString As String = encounter.Item("description", "description").ToString

              Dim newListItem As ListItem
              newListItem = New ListItem("description", "description")


              ddl.Items.Add(newListItem)
              dv.Controls.Add(ddl)
              sysData.Controls.Add(dv)

          Next

      End If

解决方案

Instantiate the ddl

Dim ddl As New DropDownList


All you have done is create a variable ddl. You need to assign a value to that variable before you can add items to it.
E.g.

Dim ddl As DropDownList = this.Page.Master.FindControl("YourDropDownList")


If this is WinForm then

Dim ddl As DropDownList = this.YourDropDownList


(you need to substitute the id of the drop down list on your page/form for "YourDropDownList") or you could use

Dim ddl As DropDownList = New DropDownList()

- but no-one is going to see your list


change

Dim ddl As DropDownList


to

Dim ddl As New DropDownList


use the New keyword to create a new instance of DropDownList

if you already added DropDownList to your aspx page like below

<asp:DropDownList ID="DropDownList1" runat="server" />


then you don't need to create new one. access it by ID, sample code

For Each encounter In ddlResult.Tables(0).Rows

          Dim ddlString As String = encounter.Item("description").ToString

          Dim newListItem As ListItem
          newListItem = New ListItem("description")
          DropDownList1.Items.Add(newListItem)

      Next


这篇关于变量'ddl'在赋值之前使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆