Reputation: 789
I have a problem with asp:hiddenfield, when I change its value in client side and wants to get it in server side , it gives me null... here is client side code :
function pageLoad() {
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
}
and I want to get the value of hiddenfield in server side code :
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = HiddenField1.Value;
}
but the result for text of button is null... why?? thanks in advance:)
Upvotes: 0
Views: 1005
Reputation: 62260
Could you try with document ready?
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
});
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function pageLoad() {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
}
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
Upvotes: 0
Reputation: 27012
After this line:
var gV = $('#<%=HiddenField1.ClientID %>').val();
gV
is a string, so gV.val("1")
doesn't make sense.
Try this:
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
Now, that shouldn't cause HiddenField1.Value
to be null... did you mean empty?
Upvotes: 1