这个问题让我真的疯了。SqlDataSource的数据绑定两次:(
在一个Asp.Net的应用程序,我有两个DropDownLists,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>
正如你所看到的,它们都必然要SQLDataSources
看起来在SqlDataSource第二的DropDownList如下:
<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";
}
为什么?
你能指定那个吗?当我第一个 开始页面时,我没有选择一个值。但即使那样,双重约束也会发生。 – AGuyCalledGerald 2010-02-08 14:52:27
@Jan:因为你的SqlDataSource被绑定到一个ControlID,当该控件加载时,它的默认值被用来绑定SqlDataSource控件(它注册为导致绑定的更改)。然后当SqlDataSource加载时,它再次绑定。我宁愿保持SqlDataSources不受限制,直到我真正需要它们。然后,我使用事件触发器来更改数据源和控件ID等。 – 2010-02-08 16:00:12