2013-03-03 12 views
5

Ho un ripetitore nel mio aspx:popolamento un ASP.NET Repeater

<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound" 
    Visible="true"> 
</asp:Repeater> 

nel lato C# del web ho scritto questa funzione:

protected void createRadioButtons(DataSet ds){ 
    List<System.Web.UI.WebControls.RadioButton> buttons = new List<System.Web.UI.WebControls.RadioButton>(); 
    foreach (DataTable dt in ds.Tables){ 
      foreach (DataRow r in dt.Rows){ 
       System.Web.UI.WebControls.RadioButton rb = new System.Web.UI.WebControls.RadioButton(); 
       rb.Text = r[1] + " " + r[2] + " " + r[3] + " " + r[4]; 
       rb.GroupName = (string)r[5]; 
       buttons.Add(rb); 
      } 
     } 
     rptDummy.DataSource = buttons; 
     rptDummy.DataBind(); 
} 

Ma quando si cerca di esso, si vede Niente. Cosa sto sbagliando?

+0

postale codice aspx del ripetitore, codice in cui il recupero dei dati, e dove stai chiamando questo metodo? –

+1

Stai vincolando un elenco di pulsanti di scelta al ripetitore invece dei dati. Se si inserisce il pulsante di opzione nella voce repeatersTemplate e si limitano a legare i dati si dovrebbe essere in grado di ottenerlo –

+1

A differenza dei siti di forum, non si utilizza "Grazie" o "Qualsiasi aiuto apprezzato" o firme su [so ]. Vedi "[Se 'Hi', 'thanks', tagline e saluti saranno rimossi dai post?] (Http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

risposta

12

Prova questo:

1 - Definire il Repeater:

<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound" > 
    <ItemTemplate> 
     <asp:RadioButtonList ID="rbl" runat="server" DataTextField="Item2" DataValueField="Item2" /> 
    </ItemTemplate> 
</asp:Repeater> 

2 - Costruire la struttura dei dati e legare il ripetitore:

List<Tuple<string,string>> values = new List<Tuple<string,string>>(); 

foreach (DataTable dt in ds.Tables){ 
    foreach (DataRow r in dt.Rows){ 
     string text = r[1] + " " + r[2] + " " + r[3] + " " + r[4]; 
     string groupName = (string)r[5]; 
     values.Add(new Tuple<string,string>(groupName, text)); 
    } 
} 

//Group the values per RadioButton GroupName 
rptDummy.DataSource = values.GroupBy(x => x.Item1); 
rptDummy.DataBind(); 

3 - Definire l'evento OnItemDataBound:

protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     IGrouping<string, Tuple<string, string>> group = (IGrouping<string, Tuple<string, string>>)e.Item.DataItem; 
     RadioButtonList list = (RadioButtonList)e.Item.FindControl("rbl"); 

     list.DataSource = group; 
     list.DataBind(); 
    } 
} 

Vedete, ogni IGrouping<string, Tuple<string, string>> si riferisce ad un gruppo di RadioButtons di una certa GroupName, ma sono anche gli elementi dal ripetitore. Per ogni articolo creiamo una nuova RadioButtonList che rappresenta l'intero gruppo di RadioButton.

si può fare meglio utilizzando un datastructure diverso da un Tuple, spesso non è chiaro che cosa Item1 e Item2 mezzi.

UPDATE:

Se si desidera visualizzare i valori selezionati:

protected void button_OnClick(object sender, EventArgs e) 
{ 
    foreach (RepeaterItem item in rptDummy.Items) 
    { 
     RadioButtonList list = (RadioButtonList)item.FindControl("rbl"); 
     string selectedValue = list.SelectedValue; 
    } 
} 
+0

+1 qualcosa di nuovo per me –

+0

Ottimo! funziona Ora, come posso "ricevere" tutti quelli selezionati? – Javi

+0

Sì, avresti per scorrere le voci del ripetitore per trovare ogni elenco di pulsanti Radio. –

1

È necessario inserire il RadioButton nel ripetitore e collegarlo all'evento createRadioButtons.

+0

Potresti essere più dettagliato? – Javi

Problemi correlati