Ehsan Kayani
Ehsan Kayani

Reputation: 113

Find Control Asp.net

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?

Upvotes: 2

Views: 10362

Answers (4)

Mike Cheel
Mike Cheel

Reputation: 13106

Try

Page.Controls.FindControl() 

or

Page.YourFormNameHEre.Controls.FindControl()

Upvotes: 0

pavanred
pavanred

Reputation: 13793

Use FindControl to search for a server control for which you have specified the id parameter.

Control ctrl = FindControl("TextBox1");

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

If you created it programmatically as a literal, you can't use FindControl to find it. When the form posts back, you can use the form collection to see if the value posted back, as in:

Request.Form["test"]

or

Request["test"]

If the user doesn't check the checkbox, then the form value will not be present, which is something the uses a hidden field to work around.

HTH.

Upvotes: 2

AsifQadri
AsifQadri

Reputation: 2388

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);
}

Upvotes: 5

Related Questions