How to fetch the dyanamically added check box ID on a button click event?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I am dynamically generating some check boxes on page load,
Here my requirement is to check whether a check box is checked or not and if it is checked fetch its ID.
I am doing it as follows:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; protected <span style="color:Blue; void Page_Load(<span style="color:Blue; object sender, EventArgs e)
{

<span style="color:Blue; if (!IsPostBack)
{
<span style="color:Blue; for ( <span style="color:Blue; int i=0;i<10;i++)
{
chkDispaly = <span style="color:Blue; new CheckBox();
chkDispaly.Text = <span style="color:#A31515; "Test" + i;
chkDispaly.ID = <span style="color:#A31515; "chkDispaly" + i;

pnlDisplay.Controls.Add(chkDispaly);
pnlDisplay.Controls.Add(<span style="color:Blue; new LiteralControl(<span style="color:#A31515; "<br>"));
}
}
}

<span style="color:Blue; protected <span style="color:Blue; void btnCreate_Click(<span style="color:Blue; object sender, EventArgs e)
{

<span style="color:Blue; foreach (Control cntrl <span style="color:Blue; in pnlDisplay.Controls)
{
<span style="color:Blue; if (cntrl <span style="color:Blue; is CheckBox)
{
<span style="color:Blue; if (((CheckBox)cntrl).Checked == <span style="color:Blue; true)
{
<span style="color:Blue; string id = ((CheckBox)cntrl).ID;
}
}

}
}
[/code]

<br/>
But on button click I am unable to fetch the controls from the panel because of post back.
How can achieve this?
Thanks,
Ram <hr class="sig reddy

View the full article
 
Back
Top