Asp.net 在SqlDataSource中注入记录
我想知道如何在我的DropdownList中插入一个记录,以提供一个“All”选项。这是我的代码,数据来自Northwind数据库Asp.net 在SqlDataSource中注入记录,asp.net,sqldatasource,Asp.net,Sqldatasource,我想知道如何在我的DropdownList中插入一个记录,以提供一个“All”选项。这是我的代码,数据来自Northwind数据库 <asp:DropDownList ID="ddlRegion" runat="server" DataSourceID="SqlDataSource1" DataTextField="RegionDescription" DataValueField="RegionID" AutoPostBack="True" onselecte
<asp:DropDownList ID="ddlRegion" runat="server" DataSourceID="SqlDataSource1"
DataTextField="RegionDescription" DataValueField="RegionID"
AutoPostBack="True" onselectedindexchanged="ddlRegion_SelectedIndexChanged" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]"
ondatabinding="SqlDataSource1_Databinding">
</asp:SqlDataSource>
我在想也许使用ondatabinding是正确的方法,但我不确定如何去做
谢谢,只需在ASPX中添加All项,然后设置AppendDataBoundItems=true,它将保留静态定义的项和数据绑定项。只需在ASPX中添加All项,然后设置AppendDataBoundItems=true,它会将静态定义的项与数据绑定项一起保留。Timbagas提出了一个好的建议,另一个建议是使用存储过程并在那里进行追加,以便返回的记录集包括“All”选项,以及所需的行。Timbagas提出了一个好的建议,另一种方法是使用存储过程并在那里进行追加,以便返回的记录集包括“All”选项,以及所需的行。谢谢,这就成功了。不幸的是,同样的技巧不适用于DataList,它没有AppendDataBoundItems属性。谢谢,这就成功了。不幸的是,同样的技巧不适用于DataList,它没有AppendDataBoundItems属性。
if(!Page.IsPostBack())
ddlRegion.Items.Insert(0, new ListItem("All", "-1"));