ContentPlaceHolder and Javascript

S

staeri

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?
 
G

Guest

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.
 
M

Mark Rae

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" :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top