为什么我要继续获得第一个ID asp.net(vb.net)下拉?

时间:2015-07-07 00:51:50

标签: asp.net vb.net

我写了一个类似下面的脚本,我的表记录显示在图片中。

enter image description here

Dim objDataTable As System.Data.DataTable = _objCommonData.fnLoadCompanyList(1)

ddlCompany.DataSource = objDataTable
ddlCompany.DataTextField = "company_name"
ddlCompany.DataValueField = "id"
ddlCompany.DataBind()

我写了这样的东西,

   If ddlCompany.SelectedValue = "" Then
       strCompany = "0"
   ElseIf ddlCompany.SelectedValue = "0" Then
       strCompany = "0"
   Else
       strCompany = ddlCompany.SelectedValue
   End If

我错过了什么?它不断返回strCompany = 1

1 个答案:

答案 0 :(得分:3)

您可能需要这样做:

If (Not Page.IsPostBack) Then
    Dim objDataTable As System.Data.DataTable = _objCommonData.fnLoadCompanyList(1)

    ddlCompany.DataSource = objDataTable
    ddlCompany.DataTextField = "company_name"
    ddlCompany.DataValueField = "id"
    ddlCompany.DataBind()
End If

有时会出现此问题,因为列表每次绑定并清除重新绑定时的选择。