从asp.net中的数据绑定下拉列表中检索选定的值 [英] retrieve selected value from databound dropdownlist in asp.net

查看:55
本文介绍了从asp.net中的数据绑定下拉列表中检索选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hye ..

我有一个包含客户记录的下拉列表.我面临一个问题,尽管用户在下拉列表中选择了任何项目,但检索到的selectedvalue是下拉列表中的最后一个项目.

这是我的代码,用于查看下拉列表中的数据.

Hye..

I have a dropdownlist which contains a customer record. I am facing a problem where although user select any item in the dropdownlist, the selectedvalue retrieved is the last item in the dropdown.

Here is my piece of code to view the data in the dropdownlist.

Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
         Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

         Me.ddlCust.Items.Add("--Select--")
         For Each dr As DataRow In dt.Rows
             Me.ddlCust.Items.Add(dr("CUSTNAME_T"))
             Me.ddlCust.DataValueField = dr("CUSTCODE_T")

         Next
         ddlCust.DataBind()



这是将所选值显示到标签中的代码



and this is the code to display the selected value into label

Me.lblCust.Text = Me.ddlCust.DataValueField 



根据以下示例,用户选择SEP,但检索到的selectedValue为03
CUSTCODE_T; CUSTNAME_T
----------- -----------
01; SEP
02; OTH
03; EPSL



Based on the example below, user select SEP, but the selectedvalue retrieved is 03
CUSTCODE_T ; CUSTNAME_T
----------- -----------
01 ; SEP
02 ; OTH
03 ; EPSL

推荐答案

我认为您想做这样的事情(假设您没有关闭viewstate):

I think you want to do something like this instead (assuming you don''t have viewstate turned off):

If Not Page.IsPostBack Then
    Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
     Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

     Me.ddlCust.Items.Add("--Select--")
     For Each dr As DataRow In dt.Rows
         Me.ddlCust.Items.Add(new ListItem(dr("CUSTNAME_T"), dr("CUSTCODE_T))
     Next

     'No need to DataBind() since you're adding the items manually
     'Also lose the DataValueField, since it's used in DataBind, which you're not using
End If



或:



OR:

If Not Page.IsPostBack Then
    Me.ddlCust.DataTextField = "CUSTNAME_T"
    Me.ddlCust.DataValueField = "CUSTCODE_T"

    Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
     Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

     Me.ddlCust.Items.Clear()
     Me.ddlCust.Items.Add("--Select--")
     Me.ddlCust.AppendDataBoundItems = True        'This keeps the item added above when you do DataBind below

     Me.ddlCust.DataSource = dt
     Me.ddlCust.DataBind()
End If




我建议您不要使用foreach循环来绑定下拉列表.
在将数据绑定到下拉列表时,您应该使用以下代码:
Hi,

I suggest you to not use foreach loop to bind the dropdown.
While binding data to the dropdown you should use this:
Me.ddlCust.DataSource=dt
Me.ddlCust.DataValueField="CUSTNAME_T"
Me.ddlCust.DataValueField = "CUSTCODE_T"
Me.ddlCust.Items.Insert(0, New ListItem("Select", String.Empty))



这可能对您有帮助.

-AK



This may help you..

-AK


这篇关于从asp.net中的数据绑定下拉列表中检索选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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