Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I am using .net2 with master page.how can I prevent control id changing while application running(e.g id 'txtName' changes to 'ctl00_ContentPlaceHolder1_txtName').Is this possible?
Posted
Updated 7-Jan-14 22:36pm
v2

You can use the ClientIDMode attribute to force the client ID of the control. If you don't want it to be changed to the auto generated name you may set it to ClientIDMode="Static".
 
Share this answer
 
Comments
Lily Dey 8-Jan-14 4:35am    
I am using .Net 2 . ClientIDMode="Static" is works in .Net framework 4.
Set ClientIDMode[^] to Static.
Quote:
Static-> The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
 
Share this answer
 
v2
Comments
Lily Dey 8-Jan-14 4:36am    
I am using .Net 2 . ClientIDMode="Static" is works in .Net framework 4.
Hi ThinkingPro,

As this is a feature of .NET4, so you can't replicate the same in .NET2.

Instead please describe what exactly you want to do with the control, so that I can help you to apply some workaround.

Thanks,
Tadit
Lily Dey 8-Jan-14 5:06am    
Actually i am doing enhancement of an existing application which was previously designed without master page.Now condition is when i have added master page all of its control id changes and weather getting value of a control via javascript function it couldn't find.
You can find using the jQuery ends with selectors.

var txtNameBox = $("input[id$='txtName']");

This is called Attribute Ends With Selector [name$="value"][^].

As the ID is 'ctl00_ContentPlaceHolder1_txtName', which ends with txtName, so this will work.

Use this technique. :)
Lily Dey 8-Jan-14 23:41pm    
In that case i have to change all existing variable that will take much time,anyway thanks your prompt reply :)
Finally i have solved this by the below code.


protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter Html = new StringWriter();
HtmlTextWriter Render = new HtmlTextWriter(Html);
base.Render(Render);
writer.Write(Html.ToString()
.Replace("name=\"ctl00_ContentPlaceHolder1_", "name=\"")
.Replace("id=\"ctl00_ContentPlaceHolder1_", "id=\""));
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900