0

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.

Andres
  • 2,013
  • 6
  • 42
  • 67
  • have you thought about adding the value to a Session variable or perhaps a Static Property..? or even `ViewState object`? `Use a Session Variable` – MethodMan Mar 15 '13 at 15:14

1 Answers1

2

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

Igor
  • 15,833
  • 1
  • 27
  • 32
Manoj Purohit
  • 3,413
  • 1
  • 15
  • 17
  • i was using the asp textbox control, but i'll try this markup. How do I set a viewstate from jquery? – Andres Mar 15 '13 at 15:50
  • you don't have to set it from jQuery, it's asp control and can maintain it's viewstate by enabling viewstate to true – Manoj Purohit Mar 15 '13 at 15:53