0

I am adding a list of radiobutton lists dynamically to the page and on button click I want to store the values . But I am unable to find the control on the page. Please find the sample code below.

for(int i=1;i<10;i++)
{

 Table tblStars = new Table();    
 RadioButtonList rb = new RadioButtonList();    
 rb.ID = i.ToString();

----
TableCell tc=new TableCell();    
TableRow tr=new TableRow();    
tc.Controls.Add(rb);    
tr.cells.Add(tc);

tblStars.Rows.Add(tr);    
ContentPlaceHolder.Controls.Add(tblStars);

}

On button click event ,

protected void btnPost_Click(object sender, EventArgs e)

 {    
    for(int i=1;i<10;i++)    
    {    
       RadioButtonList rb = (RadioButtonList)this.Page.FindControl(i.ToString());    
    }
}

Here, I am unable to find the control. FindControl is returning null.

Am I missing something here?

Thank you

rs.
  • 26,707
  • 12
  • 68
  • 90
codewarrior
  • 193
  • 1
  • 4
  • 18

2 Answers2

0

Because you are creating the RadioBuoon List dynamically you need to create them after every POSTBACK ..

Are you doing that ?

Also instead of this.Page.FindControl you need to specifically target the cell in which you are expecting it to be..

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

You are probably missing the controls when you click the button. Every time you click it, it does a postback, and Page_Load event is executed. You are probably initializing your information there and the controls in your table is reset. Try loading the controls in Page_Load event again. You could also give it a try and use view state enabled.

ragalante
  • 3
  • 3