2013-03-03 6 views
5

से पॉप्युलेट करना मैं अपने aspx में एक पुनरावर्तक है: वेब की ग # पक्ष मेंएक ASP.NET पुनरावर्तक

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

मैं इस समारोह ने लिखा है:

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(); 
} 

लेकिन जब यह कोशिश कर, यह पता चलता कुछ भी तो नहीं। मैं क्या गलत कर रहा हूँ?

+0

पद aspx कोड, कोड जहां डेटा प्राप्त करने में, और जहाँ आप इस विधि बुला रहे हैं? –

+1

आप डेटा के बजाए पुनरावर्तक के लिए रेडियो बटन की एक सूची बाध्य कर रहे हैं। यदि आप दोहराने वाले आइटम टेम्पलेट में रेडियो बटन डालते हैं और केवल उस डेटा को बाध्य करते हैं जिसे आप इसे प्राप्त करने में सक्षम होना चाहिए –

+1

फ़ोरम साइट्स के विपरीत, हम "धन्यवाद" या "किसी भी सहायता की सराहना नहीं करते हैं, या हस्ताक्षर [ ]। देखें "[चाहिए 'हाय', 'धन्यवाद,' टैगलाइन, और नमस्कार पदों से हटा दिए जाएंगे?] (Http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

उत्तर

12

इस प्रयास करें:

1 - परिभाषित 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 - डेटा संरचना निर्माण और पुनरावर्तक बाँध:

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 - परिभाषित करें 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(); 
    } 
} 

तुम देखो, प्रत्येक IGrouping<string, Tuple<string, string>> एक निश्चित GroupName की radiobuttons के एक समूह को संदर्भित करता है, वे भी पुनरावर्तक से आइटम हैं। प्रत्येक आइटम के लिए हम एक नया रेडियोबटन सूची बनाते हैं जो रेडियोबटन के पूरे समूह का प्रतिनिधित्व करता है।

आप, यह अक्सर स्पष्ट नहीं क्या Item1 और Item2 साधन है एक Tuple तुलना में एक अलग आंकड़ा संरचना का उपयोग करके बेहतर यह कर सकते हैं।

अद्यतन:

आपके द्वारा चयनित मूल्यों देखना चाहते हैं: पुनरावर्तक के

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 मेरे लिए कुछ नया –

+0

बढ़िया! यह काम करता है! अब, मैं सभी चुने हुए लोगों को "प्राप्त" कैसे कर सकता हूं? – Javi

+0

हाँ, आपके पास होगा प्रत्येक रेडियोबटन सूची को खोजने के लिए पुनरावर्तक वस्तुओं के माध्यम से पुन: प्रयास करने के लिए। –

1

आपको दोहराने में RadioButton डालना चाहिए और इसे createRadioButtons ईवेंट में बांधना चाहिए।

+0

क्या आप अधिक विस्तृत हो सकते हैं? – Javi

संबंधित मुद्दे