JavaScript your WebControls

I recently ran into an issue recently where I needed to get javascript access to controls that were created programatically to my WebControl/WebPart.  I passed the control’s ID to the javascript function, which was rendered as the page was rendered, but was still not able to get access to the control.  When I looked at the HTML rendering of the page I found that the ID of the control, when I requested it from within my C# code was not the same as what the webpart rendered in the HTML. 
 
Because the webpart class implents the INamingContainer interface all of its controls are added to the page with IDs that include namespacing from the INamingContainer interface.  This can be a major pain to get around, but there is a very simple resolution. 
 
By adding:
 
myControl.Attributes.Add("ID", myControl.ClientID);
 
I was able to override the INamingContainer’s ID attribute and use the ClientID I was expecting.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s