ContentPlaceHolder and Javascript

Discussion in 'ASP .Net' started by staeri@gmail.com, Jul 21, 2006.

  1. Guest

    I want to catch the value in "hidID" when the form on the page is
    posted but I get an error message telling me "Object is required".

    <asp:Content ID="Content1"
    ContentPlaceHolderID="ContentPlaceHolderLeft" runat="server">

    <script language="javascript">
    var a1 = document.getElementById("hidID").value; -- THIS GIVES
    ERROR MESSAGE --

    function RadioClick(id)
    {
    a1 = id;
    document.getElementById("hidID").value = a1;

    if ( a1 == "p1")
    {
    document.getElementById("p1").style.display = "block";
    document.getElementById("p2").style.display = "none";
    }
    else
    {
    document.getElementById("p1").style.display = "none";
    document.getElementById("p2").style.display = "block";
    }
    }
    </script>

    Can someone please help me?
     
    , Jul 21, 2006
    #1
    1. Advertising

  2. What is hidID, if it is an ASP.net control then it's client side ID for use
    in JavaScript is different to the ID you give it in ASP.net markup/code. The
    Id the control will have cleint side is stored in the ClientID proeprty of
    the control. You shouldn't hard code the ID of the control in your
    Javascript, instead dynamically create the javascript server side and use the
    clientID property of the control. This way if you make changes to the code
    that would affect the client side ID of the control it will not break your
    javascript.

    "" wrote:

    > I want to catch the value in "hidID" when the form on the page is
    > posted but I get an error message telling me "Object is required".
    >
    > <asp:Content ID="Content1"
    > ContentPlaceHolderID="ContentPlaceHolderLeft" runat="server">
    >
    > <script language="javascript">
    > var a1 = document.getElementById("hidID").value; -- THIS GIVES
    > ERROR MESSAGE --
    >
    > function RadioClick(id)
    > {
    > a1 = id;
    > document.getElementById("hidID").value = a1;
    >
    > if ( a1 == "p1")
    > {
    > document.getElementById("p1").style.display = "block";
    > document.getElementById("p2").style.display = "none";
    > }
    > else
    > {
    > document.getElementById("p1").style.display = "none";
    > document.getElementById("p2").style.display = "block";
    > }
    > }
    > </script>
    >
    > Can someone please help me?
    >
    >
     
    =?Utf-8?B?Y2xpY2tvbg==?=, Jul 21, 2006
    #2
    1. Advertising

  3. Mark Rae Guest

    <> wrote in message
    news:...

    >I want to catch the value in "hidID" when the form on the page is
    > posted but I get an error message telling me "Object is required".
    >
    > <asp:Content ID="Content1"
    > ContentPlaceHolderID="ContentPlaceHolderLeft" runat="server">
    >
    > <script language="javascript">
    > var a1 = document.getElementById("hidID").value; -- THIS GIVES
    > ERROR MESSAGE --


    Once your page has loaded, do a View Source and search for hidID - you'll
    see that the MasterPage / Content Page architecture has renamed it.

    So, you need add the following to your MasterPage's code:

    protected void Page_Init(object sender, EventArgs e)
    {
    this.ID = "MyMasterPage"; // or whatever name you prefer
    }

    Now, you can refer to your controls client-side as follows:

    var a1 =
    document.aspnetForm.MyMasterPage_ContentPlaceHolderLeft_hidID.value;

    or

    var a1 =
    document.getElementById("MyMasterPage_ContentPlaceHolderLeft_hidID").value;

    N.B. no matter what you name your form, the MasterPage / ContentPage
    architecture always renames it to "aspnetForm" :)
     
    Mark Rae, Jul 21, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg
    Replies:
    4
    Views:
    14,943
    dassyms
    Apr 17, 2006
  2. mosscliffe
    Replies:
    8
    Views:
    4,287
    Chris Fulstow
    Apr 7, 2006
  3. PokerMan

    ContentPlaceHolder and overlaps

    PokerMan, Mar 19, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    398
    PokerMan
    Mar 19, 2007
  4. Paul
    Replies:
    1
    Views:
    378
    Mohamad Elarabi
    Dec 20, 2007
  5. Greg
    Replies:
    1
    Views:
    228
    Ward Bekker
    Mar 23, 2006
Loading...

Share This Page