2013-05-28 16 views
8

Come impostare l'elemento selezionato di dropDownList in un ripetitore?SelectedValue of DropDownList in Repeater

Il ripetitore è associato al ripetitore DataTable Data e dropDownList è associato a dropDownList DataTable nel codice sottostante. Devo impostare la proprietà SelectedValue di DropDownList sul valore di un campo dalla tabella repeaterData.

Questo è quello che ho provato:

<asp:Repeater runat="server" ID="myRepeater> 
<ItemTemplate> 
    <asp:DropDownList runat="server" CssClass="fullSelect" ID="degree_dropdown" 
      AppendDataBoundItems="true" 
      selectedValue='<%#DataBinder.Eval(Container.DataItem,"degreeCode")%>'> 
        <asp:ListItem Text="Select Degree" /> 
    </asp:DropDownList> 
</ItemTemplate> 
</asp:Repeater> 

Codice per popolare ripetitore:

myRepeater.DataSource = myRepeaterData; //myRepeaterData is a datatable 
myRepeater.DataBind(); 

Codice per popolare DropDownList:

protected void educationPopup_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
     { 
      DropDownList degree_dropdown = e.Item.FindControl("degree_dropdown") as DropDownList; 
      if (degree_dropdown != null) 
      { 
       degree_dropdown.DataSource = degrees; //a datatable 
       degree_dropdown.DataTextField = "degree"; 
       degree_dropdown.DataValueField = "code"; 
       degree_dropdown.DataBind(); 
      } 
} 
+0

Quale evento o il metodo si usa per popolare 'degree_dropdown 'con i dati? Potresti pubblicare il codice? – Win

risposta

7

siete quasi arrivati. Hai solo bisogno di lanciare DataItem a DataRowView, e assegnarlo a DropDownList come questo -

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
     e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     var degree_dropdown = e.Item.FindControl("degree_dropdown") as DropDownList; 
     string degreeCode = (string) ((DataRowView) e.Item.DataItem)["degreeCode"]; 

     if (degree_dropdown != null) 
     { 
      degree_dropdown.DataSource = degrees; //a datatable 
      degree_dropdown.DataTextField = "degree"; 
      degree_dropdown.DataValueField = "code"; 
      degree_dropdown.DataBind(); 

      if (degree_dropdown.Items.FindByValue(degreeCode) != null) 
       degree_dropdown.SelectedValue = degreeCode; 
     } 
    } 
} 
0

Con HTML5 custom attributes, è possibile impostare il valore di discesa in un attributo di dati, e quindi impostando come valore selezionato dopo la discesa è databinded . Ho binded discesa usando asp: ObjectDataSource

<asp:Repeater runat="server" ID="myRepeater> 
<ItemTemplate> 

<asp:DropDownList runat="server" CssClass="fullSelect" ID="degree_dropdown" 
      AppendDataBoundItems="true" 
      SetValue='<%#DataBinder.Eval(Container.DataItem,"degreeCode")%>' 
datasourceid="dsCategory" datatextfield="degree" datavaluefield="code" onprerender="DropDownDataBinding"> 
        <asp:ListItem Text="Select Degree" /> 
    </asp:DropDownList> 
<asp:ObjectDataSource ID="dsCategory" runat="server" SelectMethod="LoadDegree" TypeName="WebApplication.WebForm1" /> 
</ItemTemplate> 
</asp:Repeater> 

CodeBehind

protected void DropDownDataBinding(object sender, EventArgs e) //Method to set the selected value on Category dropdown inside repeater 
{ 
    DropDownList sel = (DropDownList)sender; 
    sel.Value = sel.Attributes["SetValue"]; 
    ListItem li = new ListItem("<<Select>>", ""); 
    sel.Items.Insert(0,li); 
} 

protected DataTable LoadDegree() 
{ 
     DataTable dt = new DataTable(); 
     dt = degrees; //a datatable 
     return dt; 

} 

Il legame del vostro controllo ripetitore rimarrà lo stesso