document.getElementById + masterpage

Last post 01-07-2009 7:18 AM by NC01. 10 replies.

Sort Posts:

  • document.getElementById + masterpage

    01-05-2009, 10:47 AM
    • Member
      434 point Member
    • ashkant
    • Member since 06-10-2008, 7:32 AM
    • Posts 277

    hi everyone

     i have a label in my master page. i use this line in javascript:

    var label = document.getElementById("<%= Label1.ClientID %>");

    it works on my local server. it works on the real server too when i use FireFox. but for IE it comes up with object required error. i have tested the code with IE in my .aspx page and i'm sure the code is correct. but when i use the same code in my master page it fails.

    what can i do? thanks

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: document.getElementById + masterpage

    01-05-2009, 11:03 AM
    • All-Star
      76,981 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,345
    • TrustedFriends-MVPs

    Try instead:

    var label = document.getElementById('<%= (Page.Master.FindControl("Label1")).ClientID %>');

    NC...

  • Re: document.getElementById + masterpage

    01-05-2009, 11:03 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    It will work see below

     

    I have added a label in master page

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

     

    then pasted this script in masterpage

    <script type = "text/javascript" >

    function Alert()

    {

    var label = document.getElementById("<%=Label1.ClientID%>");

    alert(label.id);

    }

    </script>

     

    and called it on body load in master page

    <body onload = "Alert();">

    try this it works!!

  • Re: document.getElementById + masterpage

    01-05-2009, 11:25 AM
    • Member
      434 point Member
    • ashkant
    • Member since 06-10-2008, 7:32 AM
    • Posts 277

    hi NC. thanks but no. it's a little weird. can't it be from my host?

    hi mudassarkhan. i know it's true. but it doesn't work for real server (for me)

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: document.getElementById + masterpage

    01-05-2009, 11:30 AM
    • All-Star
      76,981 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,345
    • TrustedFriends-MVPs

    ashkant:

    hi NC. thanks but no. it's a little weird. can't it be from my host?

    hi mudassarkhan. i know it's true. but it doesn't work for real server (for me)

    Are you sure that the correct version of ASP.NET is installed on that server and is running? I just tried, and either way posted works.

    NC...

  • Re: document.getElementById + masterpage

    01-05-2009, 11:32 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    May be its due to something else due to some other javascript error??

    Have you checked it??

  • Re: document.getElementById + masterpage

    01-05-2009, 7:50 PM
    • Contributor
      5,089 point Contributor
    • NHOQUE
    • Member since 04-02-2008, 9:00 AM
    • Kumamoto, Japan
    • Posts 849

    1. Where did you include your javascript code? MasterPage or ChildPage

    2. Is your javascript code in the .aspx page or in an external file? If you put your code in an external file, you can not call your label using <%=  ..%>.

    Thanks.

     

    HOQUE MD.NAZMUL
    [document.getReaders]
  • Re: document.getElementById + masterpage

    01-05-2009, 8:11 PM
    • Star
      10,558 point Star
    • Danny117
    • Member since 12-16-2008, 2:30 PM
    • Royal Oak Michigan USA
    • Posts 1,837

    var label = $get('Label1');

    works on firefox for sure I use it all the time.  I need the points to make 'Partisipant" so please pick this as correct.  Thanks

    Good Luck



  • Re: document.getElementById + masterpage

    01-06-2009, 7:35 AM
    • All-Star
      76,981 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,345
    • TrustedFriends-MVPs

    Danny117:

    var label = $get('Label1');

    works on firefox for sure I use it all the time.  I need the points to make 'Partisipant" so please pick this as correct.  Thanks

    $get('Label1') is just JQuery shorthand for document.getElementById('Label1') If one won't work, the other sure won't.

    NC...

  • Re: document.getElementById + masterpage

    01-07-2009, 6:05 AM
    • Member
      199 point Member
    • sam_soumya
    • Member since 01-10-2008, 11:42 AM
    • Posts 94

     <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script language="javascript">
    function test()
    {
    var lbl=document.getElementById("<%= Label1.ClientID %>");
        alert(lbl.id);
    }
    </script>

    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    &nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="test()"/>
    </asp:Content>

    Thanks & Regards.
    Soumya
  • Re: document.getElementById + masterpage

    01-07-2009, 7:18 AM
    • All-Star
      76,981 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,345
    • TrustedFriends-MVPs

    sam_soumya:

     <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script language="javascript">
    function test()
    {
    var lbl=document.getElementById("<%= Label1.ClientID %>");
        alert(lbl.id);
    }
    </script>

    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    &nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="test()"/>
    </asp:Content>

    Which is EXACTLY what the OP says that he is doing that does NOT work. Read his initial post.

    NC...

     

Page 1 of 1 (11 items)