我有一个asp.net Visual Basic网站,我正在使用存储过程来获取值以填充下拉列表。visual basic asp.net dropDownList未填充值
但是,我有两个问题。如果我将方法填充到页面加载事件的If Not IsPostBack语句中的下拉列表中,则整个列表中将填充表示“System.Data.DataViewRow”而不是实际值的项目。如果我把它放在这个'if'声明之外,但仍然在我的页面加载事件中,我会得到正确的值,但无论我选择什么,当我离开下拉菜单时,它将恢复到顶部项目而不是所选项目。
我在做什么错?
编辑:我现在已经按照Nic的建议添加了DataTextField,并将该方法放入我的'If Not IsPostBack'事件中,正如Lee Bailey所建议的那样。然而,我的下拉菜单仍然显示所有的值为“System.Data.DataViewRow”,所以我不知道李的解决方案是否有效!关于展示价值的任何其他想法!我已在下面更新了我的代码以反映这些更改。
在Visual Basic:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bodyPanel.Visible = False
drpRegion_fill()
End If
End Sub
Protected Sub drpRegion_fill()
Dim sqlConn As SqlConnection
sqlConn = New SqlConnection
sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString
Dim drpRegionCmd As SqlCommand
drpRegionCmd = New SqlCommand("getRegionName", sqlConn)
drpRegionCmd.CommandType = CommandType.StoredProcedure
Dim drpRegionAdp As SqlDataAdapter
drpRegionAdp = New SqlDataAdapter(drpRegionCmd)
Dim drpRegionDs As DataSet
drpRegionDs = New DataSet
sqlConn.Open()
drpRegionAdp.Fill(drpRegionDs)
With drpRegion
.DataSource = drpRegionDs
.DataBind()
.DataValueField = "regionName"
.DataTextField = "regionName"
.SelectedIndex = 0
End With
sqlConn.Close()
End Sub
的标记:
<asp:Panel ID="panelRegion" runat="server" Height="160px" Width="71%" CssClass="inlineBlock">
<h2>Region:</h2>
<asp:dropDownList runat="server" AutoPostBack="true" ID="drpRegion" />
</asp:Panel>
SQL过程返回与 'regionID' 作为第1列,和 'regionName' 作为COLUMN2两列数据集。
我已经花了大约两天的时间来尝试各种各样的事情并阅读所有的书籍!我从来没有在C#中遇到过这个问题,当我执行相同的操作时,我无法为我的生活想到我在VB中错过了什么......
设置DataTextField是否有所作为? –
你在哪里设置DataTextField? – Ric