views:

264

answers:

3

Hello, this problem is driving me really mad.

In an Asp.Net-application, I have two DropDownLists, DropDownStore and 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>  

As you can see, they are both bound to SQLDataSources.

The SQLDataSource for the second DropDownList looks as follows:

<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>    

so that the second DropDownList is bound, when the user chooses an entry of the first DropDownList.

This works well. But when I set the value of the first DropDownList programmatically, the second DropDownList is bound twice:

 protected void Page_Load(object sender, EventArgs e)
 {
    storeDropDown.SelectedValue = "someStore";       
 }

Why?

A: 

While I don't know the exact reason for the double binding, your best bet might be to set an OnDataBinding handler for the second list, set a breakpoint in it, and examine the call stack in both places. That'll tell you why the framework is binding that list, both times.

Depending on how sensitive to performance you are, you might want to just set AppendDataBoundItems to false on the second list so that it doesn't matter that it's re-bound.

DDaviesBrackett
Yes, you´re right.
Jan-Frederik Carl
A: 

It's being told to bind twice. It binds on the original value of the first dropdown (since that's the controlID governing it), and then when you set it in Page_load event it binds it again.

I would recommend binding it to a label with visible property set to False. Then on the first dropdown selectedindex changed, set the text property of the label.

Joel Etherton
Could you specify that? I don´t have a value chosen when I firststart the page. But the double binding happens even then.
Jan-Frederik Carl
@Jan: Because your SqlDataSource is bound to a ControlID, when that control loads, its default value is used to bind the SqlDataSource control (it registers as a change which causes a bind). Then when the the SqlDataSource loads, it binds again. I prefer to leave the SqlDataSources unbound until I actually need them. Then I use the event trigger to change datasource and controlid, etc.
Joel Etherton
A: 

Try wrapping your selection in:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
  {
    storeDropDown.SelectedValue = "someStore";       
  }
} 
Ian Jacobs
No, it happens regardless wether the if-clause is included or not.
Jan-Frederik Carl