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
Posted

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!
   
Comments
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!
Hi

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

XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript" language="javascript">
        function showMasterPageControlID(ctrlId){
            alert(document.getElementById(ctrlId).id);
        }
    </script>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>


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

C#
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..

C#
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
   
Comments
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.
VB
by using the following code can access the controls in masterpage from popup window
window.opener.document.getElementById('ctl00_ContentPlaceHolder1_txtWSalerate').value=mytool_array[5];
   
Comments
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