Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a method for keeping the id I set for an asp .net control when it outputs to HTML?

Tags:

asp.net

xhtml

When I create a form, I try to make accessibility a top priority, but the output from asp .NET negates some of this. For example, when I set a label tag for an input tag, I create it a such:

<label for="frmFirstName">First Name<span class="required">*</span></label>
<asp:TextBox id="frmFirstName" CssClass="textbox" runat="server" />

But, when the HTML is output from the page, it is output as:

<label for="frmFirstName">First Name<span class="required">*</span></label>
<input name="ctl00$phMainContent$frmFirstName" type="text" id="ctl00_phMainContent_frmFirstName" class="textbox" />

This is a problem, as now the label is no longer tied to the input element. Is there a way to force .NET to output the exact id I set for the input field? I shouldn't have to use Javascript to correct something like this.

like image 765
Adam Avatar asked Oct 01 '10 20:10

Adam


People also ask

Which all controls in ASP.NET can be used to display text in a page?

The label control is used to display a text or a message to the user on the form. The label control is normally used along with other controls.

Which attribute is necessary for HTML control to work as a HTML server control?

The runat=”server” attribute is necessary for HTML control to work as a HTML server control.

What is a control ID?

The control ID is the unique ID of the control within the parent view. You can use the control ID to access a subview instance at run time.


1 Answers

If you use ASP.Net 4 you can set the ClientIDMode attribute of the control to static. This will let it keep the value of the ID tag when rendered.

like image 139
Mikael Svenson Avatar answered Oct 29 '22 18:10

Mikael Svenson