IT_info
IT_info

Reputation: 727

set value to hidden input

Can anyone tell me what am I doing wrong. The hiddenvalue is being kept all the time null without any value assigned to it.

The following is the code;

Javacript

This is intended to occur on a click event on a map

var str = "ryan";
document.getElementById("hiddenvalue").value = str;

This is from the asp.net

<div>
    <input id="hiddenvalue" type="hidden" value="" runat="server"/>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>

And this is how am I referring to it from the codebehind:

using (NpgsqlCommand cmd = new NpgsqlCommand("insert into altitude select nextval('altitude_id_seq'), :value", conn)) {
    cmd.Parameters.Add(new NpgsqlParameter("value", NpgsqlDbType.Varchar));
    cmd.Parameters[0].Value = hiddenvalue.Value; 
    cmd.ExecuteNonQuery();
}

For some reason no value is being set. Can anyone guide since I have been for hours trying to debug this issue.

Upvotes: 2

Views: 1429

Answers (1)

Habib
Habib

Reputation: 223237

If you can access the hiddenvalue on code behind then its a server side control, you need to access it with ClientID in Javascript

document.getElementById("<%= hiddenvalue.ClientID %>").value = str;

Or you may sepcify ClientIDMode Enumeration (static) with hiddenvalue

Upvotes: 2

Related Questions