问题
I am loading text into a textbox through jquery as so:
$.ajax({
type: "POST",
url: "../myfile.ascx/myfunction",
data: "{variable:'" + value + "'}",
contentType: "application/json",
dataType: "json",
success: function (response) {
$('input[id$=txtMyTextBox]').val(response.d);
}
});
this portion works fine, but when I cause the page to postback from other controls I loose the value it had inserted in txtMyTextBox..Now I am using a user control so I have a databind instead of page load. I tried this:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "RefreshJob();", true);
}
but that wouldn't bring back the value either. I added a hidden field which holds the value after any postback, but not sure what to do to keep the value in the textbox aswell.
回答1:
I think you are using html input element, use asp textbox control instead and pass it's ID to jquery using $('#<%=txtbox.ClientID%>').val(response.d);
and use viewstate for maintaining state of the textbox control
来源:https://stackoverflow.com/questions/15435840/preserve-value-in-textbox-after-postback-in-user-control