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.
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:37alert(window.strcountry);
inside the script tags and before thedocument.getElementById...
, it's providing value. – leodeep Nov 26 '13 at 10:59window.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 adocument.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 towindow.strcountry
. After that, doalert(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