Slacker616
Slacker616

Reputation: 855

Get values from dynamic controls with c#?

I created a few radiobuttonlist controls on my project, they're created every time the page is loaded, i want to get the value of the radiobutton that the user has selected, but since my radiobuttons were created dynamically, i don't know how to acces to their values nor how to create their event handlers. Is there a way to assign a name or id to the control when i create it? i hope you can help me.

I create a seires of radiobuttlist on the page_load event, with the text and their values been pulled out of a database. now, the user has to choose one of the options from that radiobuttlist and i want to get the value of the radiobutton the user checked. how do i do that if i don't know the name nor the id of the radiobuttlist since they're created dynamically. this is what i've got:

for (int i = 3; i < numfields; i++) {
            if (dr[i].ToString() != "" && dr[i] != null){
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString()));
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex);
            }
        }

so basically i use my datareader to loop through the data in the database, if the value from the field isn't empty or null, then i add an item to the radiobuttlist called "r" i tried to create an eventhandler for that too, but since i have never worked with them i really don't know what to do. :( I'm so sorry if i seem way too pathetic.

Upvotes: 0

Views: 1517

Answers (4)

competent_tech
competent_tech

Reputation: 44931

The problem is that you are creating the controls in page_load. In order for their values to be posted back into the controls correctly, you must move this creation into the page_init method and recreate them every time.

Then, in page_load, you can access the values in the controls correctly. If you give them IDs using a consistent naming convention, you will be able to find them using the FindControl method or, in page_init, you can store them in a collection at the page or user control level.

Upvotes: 0

dash
dash

Reputation: 91480

Taking a quick look at your code:

for (int i = 3; i < numfields; i++) { 
            if (dr[i].ToString() != "" && dr[i] != null){ 
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString())); 
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex); 
            } 
        } 

The most obvious thing that jumps out is your if statement. You should first check for null:

if (dr[i] != null && dr[i].ToString() != ""){

As if dr[i] is null, you'll get an exception (as you'll be trying to call the ToString() method on a null object.

If the contents of dr are always going to be strings, you might consider writing:

if(!String.IsNullOrEmpty(dr[i]){

I also note you start your indexing at 3 - is this because you want to skip the first 3 fields?

Wherever you create your variable, 'r', you can set the name and ID properties. You can use the ID property to look for the control on PostBack. So if you created your radiolist like so:

RadioButtonList r = new RadioButtonList();
r.Id = "MyRadioButtonList";
r.SelectedIndexChanged += MyRadioButton_SelectedIndexChanged;

Which would point at the following event handler:

private void MyRadioButton_SelectedIndexChanged(Object sender, EventArgs e) {

    ... Do Stuff ...    

}

There are several ways of finding your control when you post back; you can look in the Request.Forms collection for a control matching the name of the control you submitted, or, more appropriately, you can use the FindControl method with the ID you gave the control. See C#, FindControl for a post with a method (by Jeff Atwood!) that will search the entire hierarchy of controls for your control.

When you add a dynamic control is important, too. If you add it too late in the page lifecycle then it will not be available on PostBack. See http://support.microsoft.com/kb/317515 for more details on just when to add a control. There are plenty of resources for Dynamic ASP.Net controls around too.

Upvotes: 1

Gabriel GM
Gabriel GM

Reputation: 6639

You could put your RadioButton into a list as you create them. This is also when you want to add your handlers.

RadioButton rb;
for (int i = 1; i < 5; i++)
{
    rb = new RadioButton();

    rb.AutoSize = true;
    rb.Location = new System.Drawing.Point(25, (i*25) + 25);
    rb.Name = "radioButton" + i.ToString();
    rb.Text = "radioButton" + i.ToString();
    //Add some event handler?
    this.Controls.Add(rb);
    lstRadioButton.Add(rb);
}

Whenever you want to know which one is selected you can do a foreach loop of your list and look if your RadioButton is checked.

foreach (RadioButton rButton in lstRadioButton)
{
    if (rButton.Checked == true)
    {
        //Do something
    }
}

Upvotes: 1

Otiel
Otiel

Reputation: 18743

You are maybe searching for TagName property if the programmatic name isn't enough for you.

Upvotes: 0

Related Questions