Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I need some help on this one:

I'm creating a server control which has triggers.
(it's a context menu, and each trigger makes the context menu pop)

the triggers are passed by the user as a string (similar to the Extender's property - 'TargetControlID', or the updatepanel's "triggers")
such as:
HTML
<Triggers>
   <atm:Trigger ControlID ="Button1" />
</Triggers>

and then passed to the javascript class (this is an ajax control)

But when a masterpage is used I actually need to pass the ClientID of the control. "FindControl(trigger.ID).ClientID" doesn't work because I need to know which contentPlaceHolder the control is in...

I couldn't find out how the extenderControls got pass that problem...

Any suggestions?

thanks in advanced!
Posted
Updated 8-Jan-10 1:27am
v2

This works in C# ASP.NET - not sure what you are using.

Because master page items (like buttons, etc) are prefixed with master page references, the ID "butGo" is not visible, and cannot be accessed in Javascript, even with document.getElementbyId("butGo").
To cure this, add the following to your .CS file:

/// <summary>
/// Create a javascript array of client IDs for access via master pages.
/// In addition, creates and appends the code required to access the array and return the fully qualified name.
/// </summary>
/// <param name="wc">Controls to access via Javascript</param>
public void CreateJSArrayWithClientIds(params Control[] ca)
    {
    if (ca.Length > 0)
        {
        StringBuilder arrClientIDValue = new StringBuilder();
        StringBuilder arrServerIDValue = new StringBuilder();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Now loop through the controls and build the client and server id's
        foreach (Control c in ca)
            {
            arrClientIDValue.Append("\"" +
                             c.ClientID + "\",");
            arrServerIDValue.Append("\"" +
                             c.ID + "\",");
            }

        // Register the array of client and server id to the client
        cs.RegisterArrayDeclaration("MyClientID",
           arrClientIDValue.ToString().Remove(arrClientIDValue.ToString().Length - 1, 1));
        cs.RegisterArrayDeclaration("MyServerID",
           arrServerIDValue.ToString().Remove(arrServerIDValue.ToString().Length - 1, 1));

        // Now register the method GetClientId, used to get the client id of tthe control
        cs.RegisterStartupScript(this.Page.GetType(), "key",
           "\n" +
           "function GetClientId(serverId)\n" +
           "   {\n" +
           "   for(i=0; i<MyServerID.length; i++)\n" +
           "      {\n" +
           "      if ( MyServerID[i] == serverId )\n" +
           "         {\n" +
           "         return MyClientID[i];\n" +
           "         break;\n" +
           "         }\n" +
           "      }\n" +
           "   }\n", true);
        }
    }
Add the following to your Page_Load event:

CreateJSArrayWithClientIds(...);

where ... is the list of controls you wish to access in javascript.
e.g. CreateJSArrayWithClientIds(butGo);

Then in your javascript, use:

document.getElementById(GetClientId("id of control"));

e.g. document.getElementById(GetClientId("butGo"));
 
Share this answer
 
FindControl only looks for immediate children of a control. If the control is inside a control you will need to recurse through each control's children.
 
Share this answer
 
Use the class in your project. It contains an extension method which will iterate and find the control recursively.

public static class Extensions
    {
        public static Control FindControlR(this Control root, string id)
        {
            Control controlFound;
            if (root != null)
            {
                controlFound = root.FindControl(id);
                if (controlFound != null)
                {
                    return controlFound;
                }

                foreach (Control c in root.Controls)
                {
                    controlFound = c.FindControlR(id);
                    if (controlFound != null)
                    {
                        return controlFound;
                    }
                }
            }
            return null;
        }
    }

You can find and use the control from masterpage.
e.g.
this.FindControlR("Button1") 
 
Share this answer
 
Thank you all for the quick replys!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900