connersz
connersz

Reputation: 1213

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.

Upvotes: 0

Views: 6573

Answers (4)

rollo
rollo

Reputation: 305

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;
        }
    }

Upvotes: 1

hendryanw
hendryanw

Reputation: 1937

You can use javascript to set values without having to refresh the parent page.

Upvotes: 0

Johann Combrink
Johann Combrink

Reputation: 703

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.

Upvotes: 1

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8444

Add EnableViewState=true to each of the textboxes that you want to retain.

Upvotes: 1

Related Questions