Find Control Asp.net

Ehsan Kayani picture Ehsan Kayani · Oct 29, 2010 · Viewed 10.3k times · Source

I have created

<input type="checkbox" id="test" > 

using literal. Now i want to get this control so i can check if its checked or not. How do I find this control in the aspx.cs page?

Answer

AsifQadri picture AsifQadri · Nov 3, 2010

if you want to find control on code behind file , then you should set this as runat="server",

literal.text = "<input type=\"checkbox\" id=\"forum1\" runat=\"server\">";



HtmlInputCheckBox test = (HtmlInputCheckBox) Page.FindControl("test");

but whenever page is going to postback you lost state of this control.

May this will give you right solution http://www.codeasp.net/blogs/SumitArora/microsoft-net/841/value-of-dynamic-textbox-lost-on-postback

You can use page init event to generate control

protected override void OnInit(EventArgs e)
{ 
  HtmlInputCheckbox test = new HtmlInputCheckbox ();
  test.id= "test";                 
  pnlControl.Controls.Add(test);
  base.OnInit(e);
}