这个问题让我很生气。
在 Asp.Net 应用程序中,我有两个 DropDownList,DropDownStore 和 DropDownCampaign。
<asp:DropDownList ID="storeDropDown" AppendDataBoundItems="true"
AutoPostBack="true" DataSourceID="storeSqlDataSource"
DataTextField="Name" DataValueField="StoreId"
runat="server" OnSelectedIndexChanged="storeDropDown_SelectedIndexChanged">
<asp:ListItem Value="">Choose a store</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="campaignDropDown" DataSourceID="campaignSqlDataSource"
DataTextField="Name" DataValueField="CampaignLevelId"
AppendDataBoundItems="true" runat="server">
<asp:ListItem Value="">Choose a campaign</asp:ListItem>
</asp:DropDownList>
如您所见,它们都绑定到 SQLDataSource。
第二个 DropDownList 的 SQLDataSource 如下所示:
<asp:SqlDataSource ID="campaignSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AFPMAdManagerConnectionString %>"
SelectCommand="SELECT [CampaignLevelId], [Name] FROM [CampaignLevel] where [StoreId] = @StoreId">
<SelectParameters>
<asp:ControlParameter ControlID="storeDropDown" Name="StoreId" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
这样当用户选择第一个 DropDownList 的条目时,就绑定了第二个 DropDownList。
这很好用。但是当我以编程方式设置第一个 DropDownList 的值时,第二个 DropDownList 被绑定了两次:
protected void Page_Load(object sender, EventArgs e)
{
storeDropDown.SelectedValue = "someStore";
}
为什么?