views:

1661

answers:

6

Stop Press! This question is misleading; the code below works; the problem is unrelated. Please vote to delete. For those interested I had a self-closing <div/> that Chrome didn't like. It works fine with a <div> and </div>. I don't know if this affects other browsers, yet.

Going round in circles here! I have an asp:HiddenField in my .aspx:

<asp:HiddenField id="Aaargh" runat="server" value="Initial Value" />

It gets rendered like this in the browser:

<input type="hidden" name="Aaargh" id="Aaargh" value="Initial Value" />

I set another value dynamically in javascript:

document.getElementById("Aaargh").value = "Dynamic Value";
alert(document.getElementById("Aaargh").value); // Displays "Dynamic Value"

And then the codebehind.cs:

protected void Submit_Click(object sender, EventArgs e)
{
    Trace.Write(Aaargh.Value); // Displays "Initial Value"
}

I have also tried a plain html input type=hidden, but I have exactly the same problem?! I know I haven't written a web site for 15 years but I feel like hidden fields have stopped working in the meantime! Happy to learn of any better ways to pass client generated strings to the server! Thanks.

A: 

You're not going to get the value of the hidden field until your page "posts back" to itself or to another page after hitting the form submit button. You could get the value sent to the server async using ajax, but I'm not sure if that's what you're trying to do.

marduk
A: 

Perhaps the value of the server control is being reset on postback? I think you need to remove the "value" attribute in the asp:HiddenField and populate it some other way, perhaps using javascript.

DOK
I tried removing the asp:HiddenField value= but then Aaargh.Value == "" on postback!
Josh
+1  A: 

Maybe your problem's somewhere else because the following works fine for me:

<%@ Page Language="C#"  %>
<script runat="server">
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        Response.Write(Aaargh.Value);
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    window.onload = function() {
        document.getElementById("Aaargh").value = "Dynamic Value";
    };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField id="Aaargh" runat="server" value="Initial Value" />
        <asp:LinkButton ID="btnOk" runat="server" OnClick="BtnOK_Click" Text="OK"  />
    </div>
    </form>
</body>
</html>
Darin Dimitrov
Ok, that's really freaky because your code works for me too? What am I missing!
Josh
Yes, the problem is somewhere else!
Josh
A: 

In the GetElementById try using the ClientId of the server side control, asp.net adds to the Id to create a unique one.

document.getElementById(<%=Aaargh.ClientId%>).value = "Dynamic Value";
alert(document.getElementById(<%=Aaargh.ClientId%>).value); // Displays "Dynamic Value"
Pharabus
A: 

Onclientclick of your submit button where you create it try this....

submit.onclientclick = "setvalue("Dynamic Value")"

then your javascript function will look like this:

function setvalue(value){
document.getElementById('<%=Aaargh.ClientID%>').value = value;
}
Eric
A: 

It could be a case sensitivity issue in your HiddenField tag. Try:

<asp:HiddenField id="Aaargh" runat="server" Value="Initial Value" />
Jason
Didn't seem to make any difference, thanks.
Josh
Try adding onClientClick="alert(document.getElementById('Aaargh').value); return true;" to your submit button in order to see Aaargh's value just before the postback.
Jason