Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have one table in database and want to retrive the data and display it in web page
so this is what i know

dr = dt.Rows[k];
Label1.Text = dr[0].ToString();
RadioButton1.Text = dr[1].ToString();
RadioButton2.Text = dr[2].ToString();
RadioButton3.Text = dr[3].ToString();
RadioButton4.Text = dr[4].ToString();

please tell me how to increament this so that i should not copy and paste this again and again.please give me the code...help me...
Posted

Did I already explain you that you should not create your radio buttons in Designer? It makes no sense — manual unqualified boring unreliable work. Create them in code instead:

C#
RadioButton[] RadioButtons = new RadioButton[/*...*/];
for (int index = 0; index < RadioButtons.Length; ++index) {
    RadioButton rb = new RadioButton();
    RadioButtons[index] = rb;
    rb.Text = //... from some data file, resource, date set...
    rb.Left = //calculate it based on index and/or layout measures
    rb.Top = //calculate it based on index and/or layout measures
    rb.CheckedChanged += (sender, eventArgs) => {
        RaduoButton radioButtonSender = (RaduoButton)sender;
        //some handling using radioButtonSender and its status
    };
    rb.Parent = someRadioButtonParent; //some group or panel
}


If you do this, all operations become easy:

C#
for (int index = 0; index < RadioButtons.Length; ++index) {
    RadioButton[index].Text = dr[index];
    //...
}


—SA
 
Share this answer
 
v2
Comments
Member 8388026 16-Nov-11 3:47am    
but i have already created the radio buttons in designer part.now how to do and proceed because i have created 200 radio buttons.
Sergey Alexandrovich Kryukov 16-Nov-11 5:04am    
Damn! Un-create them! Isn't that easy? :-)
--SA
Sergey Alexandrovich Kryukov 16-Nov-11 5:07am    
What already created 200?
Even easier: through out entire application. Believe me, you will save a lot of your time. Do it until it's not too late. 200 radio buttons? This is outrageous. You will never complete the work if you continue this way. And you won't find a user who would agree to click them all. :-)

You know what? It's bad design, will be unusable. Makes it easier and more usable: use ListBox instead. Just one instance instead of each group of radio buttons.
--SA
Member 8388026 17-Nov-11 4:57am    
Can you edit the code and tell me that what exactly to write in the comment part what you have given.i m new to asp.net.still learning....
Sergey Alexandrovich Kryukov 4-Apr-12 20:08pm    
It depends on where you want to put each button and what text to right. What is not clear so far?
--SA
Try to use FindControl.


C#
for (int index = 0; index < 200; index++) 
{
Control radioButton=yourContainer. FindControl("RadioButton"+i);
if(radioButton!=Null){

radioButton.Text= dr[i].ToString();

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900