Lisa
Lisa

Reputation: 3181

How to redirect to the same current page with different passed values

I have a page which contains a textbox and a button, let's say its URL is:

default.aspx

When user enters a value (new) in the textbox and click on the button (which causes the redirection to the same page + passing the value) the URL should change to:

default.aspx?keyword=new

The problem is when the URL is:

default.aspx?keyword=new

and when I try to enter another value in the textbox the URL after redirection wont contain the new word !

Code in Page_load

   if (Request.QueryString["keyword"] != null)
            {
                TextBox1.Text = Request.QueryString["keyword"]; //For redirection from another page
                search(); 
            }

Code in onClick button

 Response.Redirect("~/Default.aspx?keyword=" + TextBox1.Text);

I've tried to debug and figured out that, on the second value entry in textbox the button post back which causes the Page_Load to be called and the URL here will be default.aspx?keyword=new so the condition result will be true and the value in the text box will get back to how it was, how should I solve that?

Upvotes: 1

Views: 2593

Answers (1)

Chidchai
Chidchai

Reputation: 101

if ( Request.QueryString["keyword"] != null )
{
    if(String.IsNullOrEmpty(TextBox1.Text))
    TextBox1.Text = Request.QueryString["keyword"];
}

Upvotes: 1

Related Questions