preserve value in textbox after postback in user control

孤人 提交于 2019-12-12 03:02:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!