0

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.

connersz
  • 1,153
  • 3
  • 23
  • 64

4 Answers4

1

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

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
1

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.

Johann Combrink
  • 692
  • 8
  • 18
1

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;
        }
    }
rollo
  • 305
  • 3
  • 14
  • p.s. EnableViewSate property is by default set to true. you can find more info on: http://msdn.microsoft.com/en-us/library /system.web.ui.control.enableviewstate%28v=vs.110%29.aspx possible solution on http://stackoverflow.com/questions/5869453/textboxes-lose-value-on-postback – rollo May 23 '14 at 12:53
0

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

hendryanw
  • 1,819
  • 5
  • 24
  • 39