简体   繁体   中英

Find dynamically created radiobuttonlist

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

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM