C# 添加所有绑定的下拉列表
我目前正在使用C#开发一个网页。我有一个C# 添加所有绑定的下拉列表,c#,asp.net,visual-studio-2010,binding,C#,Asp.net,Visual Studio 2010,Binding,我目前正在使用C#开发一个网页。我有一个DropdownList数据与我的sql数据库中的数据绑定。在dropdownlist与我的数据库绑定之后,dropdownlist中的项是userA、userB和userC。如果我选择dropdownlist中的任何一项,特定用户的数据将显示在gridview中 因此,我现在想做的是在dropdownlist中添加一个ALL。当我点击ALL时,每个用户的数据将显示在gridview中。我怎样才能做到这一点?有什么建议吗?谢谢 p/S:我不想添加任何额外的
DropdownList
数据与我的sql数据库中的数据绑定。在dropdownlist与我的数据库绑定之后,dropdownlist中的项是userA、userB和userC。如果我选择dropdownlist中的任何一项,特定用户的数据将显示在gridview中
因此,我现在想做的是在dropdownlist中添加一个ALL。当我点击ALL时,每个用户的数据将显示在gridview中。我怎样才能做到这一点?有什么建议吗?谢谢
p/S:我不想添加任何额外的按钮来显示所有的用户数据。我想进入下拉列表
这是我的代码:
WebApp.aspx:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
这是我为新网页编辑的内容。我不在代码隐藏中调用数据绑定函数
谢谢你的帮助
这就是我想要的答案:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username" AppendDataBoundItems="True">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
感谢所有试图帮助我的人。你可以做到这一点:
将DropdownList
与SQL数据绑定后使用此代码
ddlUsers.Items.Add(new ListItem("All", "all"));
ddlUsers.SelectedValue = "all";
完成后,您可以根据以下条件进行全选用户查询:
if(ddlUsers.SelectedValue == "all")
{
// your SQL query to select all users goes here.
}
在HTML标记中,您可以添加如下内容:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
如果您不希望在默认情况下选择此选项,则可以删除
Selected=“True”
。您可以做一些事情来实现这一点
1) ddlUsers.Items.Add(新列表项(“全部”、“全部”));在要绑定下拉列表的位置添加此行
2) 使用SelectedIndexChanged
下拉事件,如
protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlUsers.SelectedValue == "all")
{
//Call your SQL query from here and bind the result set with your grid.
//if you need the id's of all the items in the drop down then write a loop and form a //string with , separated valued and pass it along.
}
}
发布一些代码,你到现在为止都尝试了什么?你到底绑定到了dropdownlist什么?它是一个数据表吗?物品清单?在dropdownlist中选择ALL时,将所有项目值以逗号分隔添加到隐藏值中,并将其拆分,然后将其传递给您的方法谢谢您的帮助,但这不是我想要的。因为我把它放在页面加载中,它不起作用。我认为这是因为页面将在数据绑定之前加载。因此,在添加“ALL”之后,只有数据绑定才会显示“ALL”。或者您知道如何在数据绑定后添加“ALL”吗?您可以在调用DropDownlIst.DataBind()函数后添加它。我修改了密码。还要确保将此代码放置在条件
中(!Page.IsPostBack){//this code}
但是我的dropdownlist数据绑定是通过使用dropdownlist右边缘的箭头来实现的。我的代码中没有DropDownlIst.DataBind()函数。在DropDownlIst.DataBind()函数之后如何调用上面的代码???请通过编辑您的问题来粘贴您的下拉列表HTML标记。编辑答案。请检查。我的dropdownlist数据绑定是通过使用dropdownlist右边缘的箭头进行的。我不会在代码隐藏中修改任何用于数据绑定的代码。那么我应该在哪里添加ddlUsers.Items.add(newlistItem(“All”,“All”))???通过单击小箭头按钮,可以在不使用数据源的情况下绑定下拉列表。我的意思是写查询。如果不能,可以使用java脚本只添加此项。我已经找到了答案。我的帖子是在我问的问题之后发布的。谢谢你的帮助。