Venkatesh Karamala
Venkatesh Karamala

Reputation: 93

Accessing htmlcontrols from codebehind

Is there any way to access html controls in ASP.net code behind.

Some control similar to Findcontrol() to access the html controls. I am using

 HtmlSelect htsel1;

    htsel1 = (HtmlSelect)FindControl("stage_txt1");

but htsel1 is returning null.

Upvotes: 2

Views: 5456

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460370

You need to add runat=server and an ID.

aspx:

<select id="stage_txt1" runat=server"  >
    <option value="1">stage 1</option>
    <option value="2">stage 2</option>
    <option value="3">stage 3</option> 
    <option value="4">stage 4</option>
</select>

codebehind:

HtmlSelect myDdl = (HtmlSelect)FindControl("stage_txt1");

or just use the servercontrols like Panel instead of div or TextBox instead of HtmlInputText or DropDownList instead of HtmlSelect and so on.

If the page is the NamingContainer( they are not nested in child-controls like Repeater) you can also access them directly without to use FindControl.

HtmlSelect myDdl = this.stage_txt1;

Upvotes: 2

Kees
Kees

Reputation: 1418

Just give the html element a runat="server" and id attribute and the control will be accessable from code behind

Upvotes: 1

htxryan
htxryan

Reputation: 2961

You need the runat="server" attribute. For instance:

<div id="myServerSideDiv" runat="server"></div>

Upvotes: 1

Related Questions