ASP.NET Get by ID Override ClientID


Some times we need to use html object’s id s  in javascript or in html source in Masterpage based ,including custom content   and user controls integrated complexed web sites.you know that the object IDs (ClientID) is generated by asp.net   .  When you are using Extjs or JSon kind of stuff its hard to manage and that asp.net generated ids because its long and complex.

An example of Extjs code that need to use id of Portal html object.
Asp.net generates an id like this
“ct100” is control id parent
“cphMiddle1” comming from ContentPlaceHolder id
“ModulePlace1” comming from userControl
“Portal1” object that i want to use its id.

if we sum of those the final id like this :
ctl00_cphMiddle1_ModulePlace1_Portal1
 

if you want to reach object you can find that id just in source code of builded aspx.
var
portal = Ext.getCmp(“ctl00_cphMiddle1_ModulePlace1_Portal1”);
 

 

You can use it for more easy by using like this:
var
portal = Ext.getCmp(“<%=Portal1.ClientID %>”);

There is another way to do it by overriding ClientID and UniqueID of your component.Instead of using base.ID use this.ID

        public override string ClientID
        {
            get
            {
                return this.ID;
            }
        }
        public override string UniqueID
        {
            get
            {
                return this.ID;
            }
        }

But one exception that should be cared about of id confilict.You should extra check for control ids that not conflict in other containers when overriding ClientIDs.

 

One tip : do not use orginal id text of ide created.Example like textboxes in your project can not be standart name of “Textbox1″,”Textbox2”,”Textbox3″… you should change “txtMyControlName”,”txtMyControlAge”,”txtMyControlSurname” or etc.

 

Advertisements

About bpostaci
Sr. Support Escalation Engineer in Microsoft.

2 Responses to ASP.NET Get by ID Override ClientID

  1. sid says:

    I want to get the client ID of a custom Sharepoint field programmatically and use it in javascript.
    My javascript is to test the custom url field and looks like
    ctl00$m$g_05457355_a6e9_436a_8f04_7fe9ae92702e$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$ctl00$hyperlinkTextBox. But, I cannot hardcode the client id in ascx file as the id will be different for different lists.

    Can you please suggest?

    • bpostaci says:

      Dont use ids .you can use class attribute or custom attribute by adding them to control in c# code than on side you can get control easly with using jquery selectors.

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

%d bloggers like this: