Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got the process of getting the value of input field in c# in here:

Get value from input html in codebehind c#

I have a hidden input field in my aspx page like this:

<input type="hidden" id="lblCountry_val" runat="server" />

Where the value of the hidden field is put through jquery:

<script type="text/javascript">
$(function () {
        BindUserInfo();
    })


function BindUserInfo()
{
 document.getElementById('lblCountry_val').value = window.strcountry;
 }
</script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>

But When I am trying to get the value in Page_Load event in code behind with this: Response.Write(lblCountry_val.Value);

Nothing is being printed. How come?

EDIT I have done this by changing the hidden input field to an invisible textbox and then putting "name" attribute in the tag.

<input type="text" id="lblCountry_val" name="lblCountry_val" runat="server" style="display:none" />

And in the code behind:

var txt=Request.Form["lblCountry_val"];

Though I have not a clear idea how it was done.

share|improve this question
    
Are you sure the hidden field is being populated in the BindUserInfo() function? Try putting an alert after you write to the hidden field and check if the value is stored in it. –  DipraG Nov 26 '13 at 10:37
    
I put : alert(window.strcountry); inside the script tags and before the document.getElementById... , it's providing value. –  leodeep Nov 26 '13 at 10:59
    
The issue should not be with window.strcountry. As your hidden field is a server side control, it's ID will be modified when it renders on the client browser. So doing a document.getElementById... on the ID as is won't work. You need to get the client ID in the same way as shown in the first answer below and set the value to window.strcountry. After that, do alert(document.getElementById('<%=lblCountry_val.ClientID%>').value) to see whether the value has been actually stored in the hidden field. –  DipraG Nov 26 '13 at 11:24

4 Answers 4

up vote 1 down vote accepted

First Method - In aspx, When you set a value to html field using Java script, Field's value doesn't appear in code behind file(aspx.cs). So you have to do additional page post back for set a value to hidden field and then you can able to catch the value in code behind file.

Second Method - Using tag, submit hidden field data to relevant aspx page.Then you can catch the value using Request.Form["lblCountry_val"] array.

share|improve this answer
    
Would you please provide a piece of code for the second method? I am not getting it properly. –  leodeep Nov 26 '13 at 10:50
    
-1 because no code sample provided. –  richb01 Feb 8 at 21:44

You should write

document.getElementById('<%=lblCountry_val.ClientID%>')

This happens because in the most cases the serve side Id of a control is different from its clientId. The way to take it is the above.

share|improve this answer

Try this...

JavaScript

<script>
    $(document).ready(function () {
        var test = "1";
        $("<%=hdn_audio_length.ClientID%>").val(test);
    });
</script>

Html 

<asp:HiddenField runat="server" ID="hdn_audio_length" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" Text="Click" OnClick="button1_Click" />

C#

protected void button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = hdn_audio_length.Value;
}
share|improve this answer

Here's an example setting hidden fields on submit click.

`<script>
    $(document).ready(function () {
        $("#submit").click(function () {
            $("#<%= ccnum.ClientID%>").val($("#cc-num").val());
            $("#<%= expdate.ClientID%>").val($("#cc-exp").val());
            $("#<%= cvc.ClientID%>").val($("#cc-cvc").val());
        });
    });
</script>`
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.