views:

209

answers:

2

I have a FormView which contains 5 dynamic DropDownLists. By dynamic, I mean the lists of dropdowns can be increased or decreased - depending on how many questions I have in my DB.

When I click the save button, I'm able to retrieve and store the content of the text boxes. But I'm not able to retrieve the DropDownLists.

One of the problems is that the ID of the DropDownLists changes name after I click the Save Button.

Why? Because upon Databinding of my DropDownLists, I rename the ID's so that I can identify which is which and store the appropriate data in the DB.

I'm not sure if I'm "attacking" this all wrong.

Here's my aspx code:

<asp:FormView runat="server" (...)>
    //Lots of textfields here
  <asp:Repeater runat="server" id="myRepeater"> //Datasource is bound in codebehind.
    <ItemTemplate>
      <li>
        <div class="value"> <asp:DropDownList id="score" runat="server" OnDataBinding="hotelCriterias_DataBinding" />  </div>
      </li>
    </ItemTemplate>
  </asp:Repeater>
</asp:FormView>

Here I rename the ID's and databind the datasource

protected void myDropDownList_DataBinding(object sender, EventArgs e)
{
  DropDownList ddl = (DropDownList)(sender);
  ddl.ID = "question" + Eval("questionID").ToString(); //Rename ID to 'question1, question2...)

  Repeater rep = (Repeater)myFormView.FindControl("myRepeater");
  rep.DataSource = this.dtQuestions;
  rep.DataBind();  
}
A: 

Try using an ObservableCollection object class as an intermediary between your drop down list and DB access.

By setting the ObservableCollection based class to be the data context for the list and handling a DataContextChanged event, you will always know what data is being displayed without having to worry about keeping track of where it is displayed.

ChrisBD
A: 

When you hit save button, you need to iterate your repeater, should be like

protected void btnSave_Click(object sender, EventArgs e)
{
    if (Repeater1.Items.Count > 0) 
    { 
        for (int count = 0; count < Repeater1.Items.Count; count++) 
        {
            DropDownList ddl = (DropDownList)Repeater1.Items[count].FindControl("ddl");
           ddl.SelectedValue//
        } 
    }
}
Muhammad Akhtar