0

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.

3
  • 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. Commented Nov 26, 2013 at 10:37
  • I put : alert(window.strcountry); inside the script tags and before the document.getElementById... , it's providing value. Commented Nov 26, 2013 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. Commented Nov 26, 2013 at 11:24

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Would you please provide a piece of code for the second method? I am not getting it properly.
1

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.

Comments

0

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;
}

Comments

0

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>`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.