TextBox1.Text = Request.QueryString in Page_Load - ASP.NET C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ricklafleur
    New Member
    • Feb 2008
    • 1

    TextBox1.Text = Request.QueryString in Page_Load - ASP.NET C#

    I'm using CSharp/ASP.NET 2.0 and trying to create a simple search page that records the user query in the URL (so can be bookmark) and also re-writes the query int the original TextBox. This only works once; there after the old query gets re-written to the TextBox regardless of what is entered.

    I think this is because I'm using the Page_Load event which may being fired, replacing the new query with the one in the current URL, between time I enter new Text and click button ... but not sure how else to do it.

    Code:
    <!-- the search form -->
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
    Code:
    /* event to re-load the page with the query terms in the URL
     * but only works the first time when there is no value in the
     * URL
     */
    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Redirect("Default.aspx?q=" + TextBox1.Text, true);
    }

    Code:
    // write the q value from URL to search box
    protected void Page_Load(object sender, EventArgs e)
    {
      if (Request.QueryString["q"] != null)
      {
        TextBox1.Text = Request.QueryString["q"];
      }
    }
    Advice?

    Thanks!

    Rick
    Last edited by jhardman; Feb 13 '08, 05:48 PM. Reason: moved to .Net forum. ASP forum is for "classic" ASP
Working...