简体   繁体   中英

How to keep textbox contents after refresh

I have a form on my page with a search function, when you do a search it fills the form with the data for that record.

Within the form is another button and textbox used to look up a reference. You click the button and choose a record to place into the textbox.

The problem is that the contents of all the other textboxes are being lost when the data is sent from the search to the page, leaving only the reference box filled.

This is how the data is sent back: The record selected has an ID which is added to a session, then the parent window reloads. When it reloads the page looks for an ID in the search and then fills the screen.

EnableViewState=true添加到要保留的每个文本框中。

you can use an update panel to limit updates to certain sections of your page. see Update Panel Documentation and thus keep state of controls that should not be posted again.

Default asp.net behavior persists the values after postback, no need to use updatePanel unless partial rendering is required.

Are you sure that you aren't clearing the values in .cs file ? what is the TextMode of the textBoxes?

Tried to reproduce your scenario but failed to do so.

here is the code - aspx file code

<form id="form1" runat="server">

    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>        
        <asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox>


        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Button" OnClick="btnSearch_Click" />
    </div>
    </form>

aspx.cs file code

 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                TextBox1.Text = "Text box 1 value";
                TextBox2.Text = "Text box 2 value";
                TextBox3.Text = "password";
            }                
        }    
        protected void btnSearch_Click(object sender, EventArgs e)
        {     
            string SearchTerm = txtSearch.Text;   
            string textValue1 = TextBox1.Text, textValue2 = TextBox2.Text, textValue3 = TextBox3.Text;
        }
    }

您可以使用javascript设置值,而无需刷新父页面。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM