ASP.NET 4.6 Hosting :: Get the control’s ClientID in an external javascript file

When you have a asp.net control inside a content place holder and when the runat=”server” attribute is set, asp.net changes the client id to something autogenerated that is a combination of the content place holder and the control id. As a result you will have to get the client id dynamically when you want to retriece the value of a control through javascript.

ahp banner aspnet-01If you add the javascript directly to the asp.net markup, you can obtain the client id like this:

Notice the quotes around <%=ControlID.ClientID %>. The expression is evaluated and replaced with the client id eventhough you would believe it would be considered a string.

How to get the client id of a control in an external javascript file
In an external javascript file you can’t use the <%=ControlID.ClientID %> to get the client id of a control. You must obtain it with a minimal snipet of javascript in the markup file, and then pass it as a parameter to a function in the external javascript file.

Aspx-file markup file

External javascript file

In ASP.NET 4.0 and later you can force asp.net not the change the id of a control – it will have the same id serverside as clientside – by use of the ClientIDMode.

Best DotNetNuke Hosting Recommendation

ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers DotNetNuke hosting starts from $5. We also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable DotNetNuke Hosting, we should be your best choice.