Click here to Skip to main content
15,895,142 members
Articles / Web Development / XHTML

Master Pages and JavaScript document.getElementById

Rate me:
Please Sign up or sign in to vote.
4.66/5 (34 votes)
13 Dec 2007CPOL3 min read 284.2K   2.1K   44  
This article talks about the issues faced in accessing controls inside a Master Page from JavaScript, and points a quick solution.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Here we need to register the client ids to the client, so that the same can be used in the javascript
        // If there are nultiple controls make it by comma seperated..
        RenderJSArrayWithCliendIds(txtTest);
    }

    // This is the method used to register the array of the clientid's as well as the serverid's
    // Also this method registers the function GetClientId, which is used to get the client id provided server id is supplied
    public void RenderJSArrayWithCliendIds(params Control[] wc)
    {
        if (wc.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
            for (int i = 0; i < wc.Length; i++)
            {
                arrClientIDValue.Append("\"" + wc[i].ClientID + "\",");
                arrServerIDValue.Append("\"" + wc[i].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", "\nfunction GetClientId(serverId)\n{\nfor(i=0; i<MyServerID.length; i++)\n{\nif ( MyServerID[i] == serverId )\n{\nreturn MyClientID[i];\nbreak;\n}\n}\n}", true);            
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
India India
An Electronics Engineer by curricula, Software Engineer by profession. Have passion towards developing applications using Microsoft's .NET technology.

Comments and Discussions