views:

40

answers:

4

I need my MasterPage to be able to get ControlIDs of Controls on ContentPages, but I cannot use <%= xxx.CLIENTID%> as it would return an error as the control(s) might not be loaded by the contentplaceholder.

Some controls have a so called BehaviourID, which is exactly what I would need as they can be directly accessed with the ID: [Asp.net does always create unique IDs, thus modifies the ID I entered] Unfortunately I need to access

e.g. ASP.NET Control with BehaviouraID="test"
....
document.getElementById("test")

if I were to use e.g. Label control with ID="asd"
....
document.getElementById('<%= asd.ClientID%>')

But if the Labelcontrol isn't present on the contentpage, I of course get an error on my masterpage. I need a solution based on javascript. (server-side)

Thx :-)

+1  A: 

You could use jQuery and access the controls via another attribute other than the ID of the control. e.g.

<asp:Label id="Label1" runat="server" bid="test" />

$('span[bid=test]')

The jQuery selector, will select the span tag with bid="test". (Label renders as span).

Tim B James
Keep in mind this is not XHTML compliant. Will be fixed in HTML5 with custom attributes (data-bid="test")
RPM1984
A: 

You could write an json-object with all the control-ids which are present on the content-page and "register" that object in the global-scope of your page.

Some pseudo pseudo-code, because I can't test it at the moment...

void Page_Load(object sender,EventArgs e) {
    System.Text.StringBuilder clientIDs = new System.Text.StringBuilder();

    IEnumerator myEnumerator = Controls.GetEnumerator();
    while(myEnumerator.MoveNext()) {
        Control   myControl = (Control) myEnumerator.Current;
        clientIDs.AppendFormat("\t\"{0}\" : \"{1}\",\n", myControl.ID, myControl.ClientID);
    }

    page.ClientScript.RegisterStartupScript(page.GetType(),
                                            "ClientId",
                                            "window.ClientIDs = {" + clientIDs.ToString().Substring(0, clientIDs.ToString().Length - 2) + "};",
                                            true);
}
john_doe
A: 

It sounds like your issue is that you are using the master page for something it wasn't intended. The master page is a control just like any other control, and therefore cannot access any of the controls of its parent (the page). More info:

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

My suggestion is to inject the JavaScript from your page where the controls can actually be resolved. Here is a sample of how this can be done:

#Region " LoadJavaScript "

        Private Sub LoadJavaScript()

            Dim sb As New StringBuilder

            'Build the JavaScript here...
            sb.AppendFormat("  ctl = getObjectById('{0});", Me.asd.ClientID)
            sb.AppendLine("  ctl.className = 'MyClass';")

            'This line adds the javascript to the page including the script tags.
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "MyName", sb.ToString, True)


            'Alternatively, you can add the code directly to the header, but
            'you will need to add your own script tags to the StringBuilder before
            'running this line. This works even if the header is in a Master Page.
            'Page.Header.Controls.Add(New LiteralControl(sb.ToString))

        End Sub

#End Region
NightOwl888
A: 

Best solution so far:

var HiddenButtonID = '<%= MainContent.FindControl("btnLoadGridview")!=null?    
MainContent.FindControl("btnLoadGridview").ClientID:"" %>';
if (HiddenButtonID != "") {
    var HiddenButton = document.getElementById(HiddenButtonID);
    HiddenButton.click();
}

Where MainContent is the contentplace holder.

By http://forums.asp.net/members/sansan.aspx

dll32