Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Tag ClientID in a .NET Project

Tags:

.net

asp.net

If I want to manipulate an HTML tag's properties on the server within an aspx page based on a master page i.e.

<a href="#" runat="server" ID="myLink">My Link</a>

For example to give the link a different class depending on the current page i.e.

if (Path.GetFileName(Request.PhysicalPath) == "MyPage")
{
myLink.Attributes.Add("class","active");
}

.NET changes the ID property of the link to something like

<a href="#" ID="ct100-foo-myLink">My Link</a>

Is there any way of stopping this from happening and keeping the original ID?

Thanks in advance

like image 789
Nick Allen Avatar asked Sep 22 '08 11:09

Nick Allen


People also ask

What is clientId in HTML?

When a Web server control is rendered as an HTML element, the id attribute of the HTML element is set to the value of the ClientID property. The ClientID value is often used to access the HTML element in client script by using the document.getElementById method. The ID is also often used in CSS rules to specify elements to style.

What is clientidmode in ASP NET?

Abstract: In ASP.NET 4.0, Microsoft has given developers the control to render client side ID of Server controls, by introducing a new property called ‘ClientIDMode’. In this article, we will explore this property. In ASP.NET, each web control is identified uniquely using the ‘ID’ property of the control.

How do I generate The clientId property value in ASP?

ASP.NET provides multiple algorithms for how to generate the ClientID property value. You select which algorithm to use for a control by setting its ClientIDMode property. The algorithms are identified by the ClientIDMode enumeration values that are listed in the following table.

How to get a reference to the HTML element in client script?

To get a reference to the HTML element that is rendered for the Label control in client script, you must know the value of the control's ClientID property. However, because the user control can be put anywhere in a Web page, it is impossible to know in advance which naming containers will contain the controls.


3 Answers

It's not possible directly. You could make a new control that inherits the link and override its ClientID to return directly the ID. But this seems to be overkill. You can simply use HTML markup and use <%# GetClass() %> to add the class when you need it.

Regarding the usage of ClientID for Javascript:

<a ID="myLink" runat="server">....


var ctrl = document.getElementById('<%# myLink.ClientID %>');

Of course you need a DataBind somewhere in the code.

like image 199
rslite Avatar answered Oct 06 '22 05:10

rslite


AFAIK there is no way.

It shows the actual control tree, which is in this case masterpage-content-control.

However if you add an ID to the masterpage (this.ID = "whatever") then you will see "whatever" instead of ctl00 (which means control index 0).

like image 42
Biri Avatar answered Oct 06 '22 05:10

Biri


I just discovered an easy way to do it if you are using jQuery.

var mangled_name = $("[id$=original_name]").attr("id");

So if you have a server control like this:

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

You can get the mangled name in your jQuery/Javascript with:

var start_date_id = $("[id$=txtStartDate]").attr("id");

Since it is a string, you can also use it "inline" instead of assigning it, e.g. concatenating it with other elements for selectors.

like image 42
CMPalmer Avatar answered Oct 06 '22 06:10

CMPalmer