Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnSet_Click(object sender, EventArgs e) 
  { 
         CheckBox chkBx = new CheckBox(); 
         chkBx.ID = "chk1"; 
      appentDiv.Controls.Add(chkBx);
  } 

protected void btnGet_Click(object sender, EventArgs e) 
{ 
       CheckBox n = this.appentDiv.FindControl("chk1") as CheckBox;  
} 

Could any please help to get checkbox value from here...?
Please reply...

Regards
Sujeet Singh
Posted
Updated 6-May-13 20:28pm
v3
Comments
KM Perumal 7-May-13 2:24am    
Improve ur question
Sujee1 7-May-13 2:29am    
In my page i have 2 buttons GET and Set and when i click on Set, a checkbox is genrated at runtime, after check the checkbox when I click on Get, I want to get the checkbox's current value...I mean Is it checked or not??
vijay__p 7-May-13 2:36am    
Are you getting checkbox object or not?
Sujee1 7-May-13 2:38am    
I'm getting...but i am unable to get its current value(checked or Not checked)..???
Bikash Prakash Dash 7-May-13 3:16am    
can you please specify what is appentDiv, which type of control is it.

Its working for me
C#
protected void btnGet_Click(object sender, EventArgs e) 
{ 
       CheckBox n = (CheckBox)this.appentDiv.FindControl("chk1") ;  
} 
 
Share this answer
 
v2
Comments
Sujee1 7-May-13 3:57am    
But it is not working for me...Are u getting any value on object n ?
KM Perumal 7-May-13 3:59am    
Ya i got true or false
Sujee1 7-May-13 4:05am    
Could you please check my code, where is the problem and why am i not getting true or false?
Here is my aspx code.

<asp:Button ID="btn" runat="server" Text="SET" onclick="btnSet_Click" />
<div id="appentDiv" runat="server">
</div>
<asp:Button ID="Button1" Text="Get" runat="server" onclick="btnGet_Click" />

please..
KM Perumal 7-May-13 4:55am    
Ur getting ispostback problem ..i tried load event with is postback igot values
Sujee1 7-May-13 7:01am    
Here is my code please solve my problem....


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}

protected void btnSet_Click(object sender, EventArgs e)
{
CheckBox chkBx = new CheckBox();
chkBx.ID = "chk1";

appentDiv.Controls.Add(chkBx);


}

protected void btnGet_Click(object sender, EventArgs e)
{
CheckBox n = (CheckBox)this.appentDiv.FindControl("chkBx");

}
You will be getting null at
C#
this.appentDiv.FindControl("chk1") as CheckBox


The reason is Dynamically added controls will not be available after postback....
So you need to handle this by using Session or Viewstate...
Please check these links...

Solve Dynamic Control Problem With PostBack[^]

http://chiragrdarji.wordpress.com/2009/05/20/maintain-viewstate-for-dynamic-controls-across-the-postback/[^]

http://www.codestructs.com/tag-blogger-com-1999-blog-2555797619867133640-post-2780447821170012635[^]
 
Share this answer
 
Comments
Sujee1 7-May-13 6:46am    
ok but i'm unable to find..could you please explain here, because it will be a very small code.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

}
}

protected void btnSet_Click(object sender, EventArgs e)
{
CheckBox chkBx = new CheckBox();
chkBx.ID = "chk1";

appentDiv.Controls.Add(chkBx);


}

protected void btnGet_Click(object sender, EventArgs e)
{
CheckBox n = (CheckBox)this.appentDiv.FindControl("chkBx");

}


Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900