One of the trickiest things that WebForms developers have to deal with is the management of HTML control identifiers by ASP.Net. In versions of ASP.Net prior to 4, the id attribute of an HTML control would always be generated at run-time and developers had little chance of guessing what the value would be If you were so lucky as to guess correctly, you would be able to properly reference your controls and life was good. If the structure of your page ever changed, ASP.Net would happily generate a new and different id for you. Now all of your client-side code is broken and that makes developers ANGRY!
"<%= txtFirstName.UniqueID %>"
"<%= txtFirstName.ClientID %>"
"<%= txtLastName.UniqueID %>"
"<%= txtLastName.ClientID %>"
clientId = controls.txtFirstName.ClientID;
I use this in many of my ASP.Net pages, especially pages that use AJAX controls that I want to make even more impressive for my end-users.
Is this a technique that you find useful? Write back in the comments area below to let me know what you think.
Jeffrey T. Fritz is a Microsoft MVP in ASP.Net and an ASPInsider with more than a decade of experience writing and delivering large scale multi-tenant web applications. After building applications with ASP, ASP.NET and now ASP.NET MVC, he is crazy about building web sites for all sizes on any device. You can read more from Jeffrey on his personal blog or on Twitter at @csharpfritz. Google Profile CodeProject
Copyright © 2016, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.