简体   繁体   English

查找动态创建的单选按钮列表

[英]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. FindControl返回null。

Am I missing something here? 我在这里想念什么吗?

Thank you 谢谢

Because you are creating the RadioBuoon List dynamically you need to create them after every POSTBACK .. 由于您是动态创建RadioBuoon列表,因此需要在每个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.. 同样, this.Page.FindControl您还需要专门针对期望它所在的单元格。

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. 每次单击它都会进行回发,并执行Page_Load事件。 You are probably initializing your information there and the controls in your table is reset. 您可能正在此处初始化信息,并且表中的控件已重置。 Try loading the controls in Page_Load event again. 尝试再次加载Page_Load事件中的控件。 You could also give it a try and use view state enabled. 您还可以尝试启用它并使用视图状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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