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

Denaes

Well-known member
Joined
Jun 10, 2003
Messages
956
Im 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 Ajaxs
Code:
$get(OriginalControlID)
they wont find a reference to the control. If it were an HTML control, this wouldnt be a problem as ASP.Net 2.0 doesnt rename their IDs.

The problem Im 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 Ive seen (AppDev videos, Ajax.ASP.Net or documentation) they use either an html control or hardcode the generated name.

Im looking for something via Javascript or MSAjax Javascript that would get me the "XXXXX_XXXXX_ControlInstanceName" portion that I dont 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? Ive been googling for days and maybe Im not using the correct terms, but Im not finding anything. Id 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 youre writing a custom control, then the script is emitted as part of the controls 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) {
        //Didnt 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 controls ClientID property.

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

Seems odd that Atlas wouldnt address this as theyre trying to make clientside coding easier for 2005 and super integrated for "Orcas"
 
Back
Top