How do you refer to serverside controls via client side code?

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
I'm using asp.net 2.0 and the Ajax Web Extensions.

I create a control with a label (lblStudentID) and when the page renders, it becomes XXXXX_XXXXX_ControlInstanceName_PanelInstanceName_OriginalControlID

If I use Javascripts
Code:
document.forms[0].getElementById(OriginalControlID)
or Microsoft Ajax's
Code:
$get(OriginalControlID)
they wont find a reference to the control. If it were an HTML control, this wouldn't be a problem as ASP.Net 2.0 doesn't rename their ID's.

The problem I'm seeing is that in my control, I only have control over the PanelInstanceName & OriginalControlID. I have no idea which control(s) (if any) that my usercontrol will be used in.

Is there a programatical way to determine what that name would be? We were hoping that MSAjax would take the ServerSideID and find the ClientSideID, but no dice. On all examples I've seen (AppDev videos, Ajax.ASP.Net or documentation) they use either an html control or hardcode the generated name.

I'm looking for something via Javascript or MSAjax Javascript that would get me the "XXXXX_XXXXX_ControlInstanceName" portion that I don't know about and would allow me to append the portion of the name I will know.

Something like
Code:
$get(ControlParent + "_PanelInstanceName_OriginalControlID"
that would allow me to create the dynamic client name.

Anyone have any tips or advice? I've been googling for days and maybe I'm not using the correct terms, but I'm not finding anything. I'd greatly appreciate any help on this subject
 
Finding controls by ID

The correct approach is to not hard-code any IDs in your Javascript and instead have the IDs written into an embedded script block on the page when it is rendered. If you're writing a custom control, then the script is emitted as part of the control's rendering. This allows the control to look up the exact ID of the target control for use in the script.

In the Ajax control toolkit the ExtenderControlBase class has a FindControlHelper method which basically works like this:

C#:
protected Control FindControlHelper(string id)
{
    Control c;
    Control nc;

    c = base.FindControl(id);  //Should work
    nc = NamingContainer;

    //Move up the naming hierarchy
    while ((c == null) && (nc != null)) {
        c = nc.FindControl(id);
        nc = nc.NamingContainer;
    }

    if (c == null) {
        //Didn't find the control, so try something else...
    }

    return c;
}

Basically this looks up the naming hierarchy for the control with specified ID. Unless the ID has been wrongly specified, it should be found. Then, the actual ID of the control can be rendered into the script block on the page, using the control's ClientID property.

Good luck :cool:
 
Well thanks for that. That should work. :)

Seems odd that Atlas wouldn't address this as they're trying to make clientside coding easier for 2005 and super integrated for "Orcas"
 
Back
Top