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
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.
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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With