Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific ID for server controls in an ASP.NET Web Form that is using a MasterPage?

Is it possible to set a specific ID on an ASP.NET server control? Everytime I assign an ID and run the web form the ID changes.

For Example:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

Gets translated into this:

<input id="ct100_ContentPlaceHolder1_txtName" type="text" />

I think this is do to me using master pages, but if so how can I be sure a control will have a certain ID(for javascript purposes). I placed the auto-generated id in my javascript and it is working, but I would prefer to have used the id's that I originally assigned them. Is this possible?

(This is for version:ASP.NET 3.5)

like image 502
Edward Avatar asked Jul 21 '10 22:07

Edward


People also ask

How can you register a custom server control to a Web page?

You can register a custom server control to a Web page using the @Register directive. Create an @ Register directive that includes: A TagPrefix attribute, which associates a prefix with the user control. This prefix will be included in opening tag of the user control element.

How are Web server controls rendered?

For example, a RadioButtonList Web server control might be rendered in a table or as inline text with other markup. Web server controls include traditional form controls such as buttons and text boxes as well as complex controls such as tables.

How many Web server controls are there in ASP.NET Explain with examples?

ASP.NET - Server Controls There are three kinds of server controls: HTML Server Controls - Traditional HTML tags. Web Server Controls - New ASP.NET tags. Validation Server Controls - For input validation.


1 Answers

Starting with .NET 4 you have greater control about how the client-side IDs look like (see this post for details).

To force a specific client-side ID, you have to set the ClientIDMode to static. The following will render an <input> element with id="txtName":

<asp:TextBox ID="txtName" ClientIDMode="static" runat="server"></asp:TextBox>

Although if you do this, you have to ensure that you don't have two controls with identical client-side IDs. Check the article linked above for other options.

like image 128
M4N Avatar answered Sep 19 '22 11:09

M4N