Click here to Skip to main content
14,928,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can access controls in master page using javascript.the javascript function is calling from a page without masterpage.i am alredy used the code
document.getElementById('ctl00_ContentPlaceHolder1_ddlItem').options[0].value =selected;
document.getElementById('ctl00_ContentPlaceHolder1_ddlItem').options[0].text =item;

but it is not working

If you're having a page that does not have a master page why would you think there'd be anything from a master page rendered to HTML.
Please explain!
Albin Abel 2-Mar-11 3:47am
I haven't noticed his point of page without master page. In that case the master page not render and the master page class instance also not available for him. My 5
Manfred Rudolf Bihy 2-Mar-11 5:17am
Thank you!

As an example..

I am having a button in master page..

<div> <asp:Button ID="Button25" runat="server" Text="Master Button" /></div>

Now in the content page I am having a script and a button

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript" language="javascript">
        function showMasterPageControlID(ctrlId){
    <asp:Button ID="Button1" runat="server" Text="Button" />

Now I assign the script to the button click event from the code behind as follows

protected void Page_Load(object sender, EventArgs e)
    Button1.Attributes.Add("onclick", "showMasterPageControlID('" + this.Master.FindControl("Button25").ClientID + "')");

This works..

But if you want to access the master control at page_load event then you may need to inject the script as follows..

protected void Page_Load(object sender, EventArgs e)

     ClientScript.RegisterStartupScript(this.GetType(), "Script1", "<script type='text/javascript'>document.getElementById('" + this.Master.FindControl("Button25").ClientID + "').value='Master Button Modified';</script>");
     //Button1.Attributes.Add("onclick", "showMasterPageControlID('" + this.Master.FindControl("Button25").ClientID + "')");

For setting multiple controls in that way use a string builder to create your script text and get the text from stringbuilder's toString method.

Use the ClientID property instead of using the way you identify the controls. Because that id change easily when the html tree hierarchy alters.

Hope this helps
Albin Abel 2-Mar-11 3:46am
oh sorry. I haven't noticed page without master page. Ignore my answer. it is for Page with master page.
by using the following code can access the controls in masterpage from popup window
BrijMal Infotech 28-Sep-16 8:00am
This Code is Master page

And this code is Content Page
<asp:TextBox ID="TextDate" runat="server" ReadOnly = "true">

But TextDate is not finding

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