Link to home
Start Free TrialLog in
Avatar of Valimai
Valimai

asked on

How to change client ID of client side control

hi there,

I have the following html
<ul id="123">
</ul>

How can i change the ID of UL in the code behind?

I tried this which obviously breaks many rules and doesn't work.
<ul id="<asp:Literal ID="litClientID" runat="server" />">
</ul>

thanks
Avatar of Type25
Type25

The clientid is automatically set for you.

What's the reason for wanting to change it, i might be able to come up with an alternative idea.
ASKER CERTIFIED SOLUTION
Avatar of discon0
discon0
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Valimai

ASKER

hi there, thanks for your replies.

@gocemi
thats a cool example. since my problem is dynamic and recursive; i can't use yours. (Ill reserve a few points for that one).

@discon0
I tried to change the ID using control.ID = "newID", but it didn't change on the output. I suspect this has to do with being in a repeater control. i did a test outside the control and the solution works. Just not from within a repeater for some reason.  I couldn't find any properties on the repeater to help.

Do you have any ideas?
You can create a list( hash map) that will contain controls whom id's needs to be changed, if controls are created dynamically.

key, value should be [id of the control], [new id to be changed]

And in here you can do it li like this.
HasMap hm = new HashMap();
... fill in the hm
...
public override string ClientID
{
get
{
if(hm.Contains[this.ID])
return hm[this.Id]
else
return this.clientid
}


I havent tried this but since client ids are assigned in the Render method i'm sure you'll have that list already.
If the controls are runat="server" and in a repeater you can use this:

      protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
            TextBox tb = e.Item.FindControl("txtTest") as TextBox;
            if (tb != null)
            {
                  tb.ID = "myNewId";
            }
      }

However, the resulting IDs will change from:
"rep_ctl00_txtTest" to "rep_ctl00_myNewId"

The "rep_ctl00_" prefix will still be there. This is being done on all controls contained within a control that has the INamingContainer interface to avoid duplicate ids across multiple controls a page might have.

Would you consider an alternative approach? If all you want is to set the IDs to something you control so you can use them from client-side javascript you could try this:

      protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
            TextBox tb = e.Item.FindControl("txtTest") as TextBox;
            if (tb != null)
            {
                  ClientScript.RegisterArrayDeclaration("textBoxes", "'" + tb.ClientID + "'");
            }
      }

this will register a client-side javascript array named "textBoxes" with all of the client Ids:

>>> textBoxes
["rep_ctl00_txtTest", "rep_ctl01_txtTest", "rep_ctl02_txtTest", "rep_ctl03_txtTest", "rep_ctl04_txtTest", "rep_ctl05_txtTest", "rep_ctl06_txtTest", "rep_ctl07_txtTest", "rep_ctl08_txtTest", "rep_ctl09_txtTest"]