【问题标题】:Dropdownlist population error from dataset asp .net来自数据集 asp .net 的下拉列表填充错误
【发布时间】:2012-08-07 01:53:10
【问题描述】:
我无法在下拉列表中填充数据集。我想在下拉列表中显示我的数据库表中的用户名列,但只显示最后一行值。我被卡住了,搜索了几个小时后我找不到任何解决方案,每个代码几乎都给了我同样的问题。
这是我想要做的:
Dataset ds = new Dataset();
ds = db.search();
if(ds.Tables[0].Rows.Count>0)
{
DropDownList1.DataSource=ds;
DropDownList1.DataBind();
}
在 ASP 标记中:
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
【问题讨论】:
标签:
asp.net
drop-down-menu
dataset
【解决方案1】:
请设置下拉列表的属性,如服务器端代码所示。请将“用户名”和“ID”替换为检索记录集列名称。
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "UserName";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
【解决方案2】:
Using ds As DataSet = data_readers.data.ExecuteDataSet("LoadSearch", NOTHING)
If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then
DropDownList1.DataSource = New DataView(ds.Tables("myDataset"))
DropDownList1.DataBind
End If
End If
End Using
【解决方案3】:
您的“ASP 标记”完全是空的!这里看看我的代码,它应该是你源页面的一个很好的例子:
您还需要设置一个对象数据源:我在下拉列表下方提供了该代码。 Listitem 值只是我的偏好。在您的情况下,您的列名将替换 TRANS_CD_DESC。
<asp:DropDownList ID="ddlTRANS_CD_DESC" DataSourceID="OBJ_TRANS_CD_DESC" DataValueField="TRANS_CD_DESC"
DataTextField="TRANS_CD_DESC" AppendDataBoundItems="true" runat="server" Text='<%# bind("TRANS_CD_DESC") %>'>
<asp:ListItem Value="">-- Select One --</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="OBJ_TRANS_CD_DESC" runat="server" TypeName="project1.dbqry"
SelectMethod="gProjectCIS"></asp:ObjectDataSource>