Pregunta

Initially, ASP.NET page renders(or set) an empty value on Page_Load and later on JQuery method sets the value when an user inputs data. I'm trying to get the value that is set by JQuery method when ASP.NET Button is clicked.

I set the value in Hidden Field using JQuery

$('#MyHiddenField').val('valueToStore');

And I'm trying to get the value from code behind C#

protected void Page_Init(object sender, EventArgs e)
{
   var myValue = MyHiddenField.Value 
}

But the myValue is always empty

What am i missing?

¿Fue útil?

Solución

Try this:

try to het value on button click

protected void Button_Click(object sender, EventArgs e)
{
   var myValue = MyHiddenField.Value 
}

Otros consejos

HTML

<asp:HiddenField ID="xml" runat="server" />

JQUERY

Sets Value to the hidden field.

$(document).ready(function () {
var booking="FEtch value from asp.net hidden field";
$("#<%= xml.ClientID %>").val(booking);
}

Retrieving Value from Hidden Field

C#

In button click

string result=xml.value;//xml is the id of hidden field.

Notes

1.Priority is must.
2.Assigning values must be done first only then fetching will not return null values.
3.Give break points and test it.

If you found this as useful and cleared your issues mark as answer and give upvotes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top