2

I have developed a website in C# asp.net which contains a Master Page. I have designed a top menu with Search bar(txtSearch) and a Button(LinkButton). When I click on search Button it redirects to the page where searched data is to be displayed but when it redirects it clear's the txtSearch. What I want is not to clear the txtSearch on page redirect or page reload.

Top Menu (Search bar)

<asp:TextBox runat="server" ID="txtSearch" TextMode="SingleLine" CssClass="form-control" Placeholder="Search term..." />
<span class="input-group-btn">
    <asp:LinkButton runat="server" ID="btnSearch" CssClass="btn btn-default" type="button" OnClick="btnSearch_Click"><span class="glyphicon glyphicon-search"></span></asp:LinkButton>
</span>

I Hope that the question is clear.

Frost_Mourne
  • 342
  • 4
  • 22
Jay Patel
  • 95
  • 1
  • 12

4 Answers4

1

What you can do is.. Store the TextBox value in Session in onclick() Function

protected void btnSubmit_Click(object sender, EventArgs e)
{
   Session["TextBoxVal"] = TextBox1.Text;//The particular TextBox that has value
}

and onPageLoad just assign the value to the TextBox value like this

 protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
            TextBox1.Text = (string)ViewState["TextBoxVal"];
       }
         //use this as per your needs just an example how to use
        if(IsPostBack)
        {
            TextBox1.Text = (string)ViewState["TextBoxVal"];
        }
    }

and once it is assigned value to the TexBox clear the session or it will keep assigning the same value.. do it like this

Session["TextBoxVal"] = null;
Frost_Mourne
  • 342
  • 4
  • 22
  • How to add that that value back to txtSearch(of master page) from Search.aspx page ? @Frost_Mourne – Jay Patel Jun 27 '17 at 07:02
  • Same TextBox1.Text = (string)ViewState["TextBoxVal"]; session can be used in any page of the Solution. just give the proper TextBox Id and u can use it – Frost_Mourne Jun 27 '17 at 07:03
  • I know that Session value can be retrieved in any page but how to retrieve the textbox(txtSearch of Master Page) ?? – Jay Patel Jun 27 '17 at 07:04
  • (string)ViewState["TextBoxVal"]; this just gives u the string which u previously saved u can give it to everything that can hold data u can give it to even label.Text= (string)ViewState["TextBoxVal"]; or anyother element – Frost_Mourne Jun 27 '17 at 07:10
  • 1
    Thank you for the Help @Frost_Mourne I used like this and it worked completely fine. if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString["keyword"].ToString(); } – Jay Patel Jun 27 '17 at 07:14
  • You are Welcome – Frost_Mourne Jun 27 '17 at 07:15
1

you have to pass data to another page.to pass data you can use various techniques

  • Query string
  • Session
  • Cookies

pass data using above one method.

i will suggest Query string will be best option

ishan joshi
  • 270
  • 3
  • 13
  • 1
    [link](https://stackoverflow.com/questions/17515199/passing-values-between-pages) for passing data – ishan joshi Jun 27 '17 at 07:06
  • Thank you for the help. It worked if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString["keyword"].ToString(); } – Jay Patel Jun 27 '17 at 07:16
-1

You must pass value in querystring in between pages to persist it.

Vijay Raheja
  • 290
  • 2
  • 10
  • Please add some explanation to your answer. Or maybe post it as a comment. – ItamarG3 Jun 27 '17 at 06:32
  • I am passing the value of txtSearch in query string but how to add that that value of query string back to txtSearch(of master page) from Search.aspx page ? @Vijay Raheja – Jay Patel Jun 27 '17 at 07:01
  • 1
    [link](https://stackoverflow.com/questions/17515199/passing-values-between-pages) for passing data – ishan joshi Jun 27 '17 at 07:07
  • Thank you for the help it worked if(Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "") { txtSearch.Text = Request.QueryString["keyword"].ToString(); } – Jay Patel Jun 27 '17 at 07:15
  • If you still have your code not running, then post your code so that i could look in. – Vijay Raheja Jun 27 '17 at 08:10
-1

put it on a session:

Session["Sample"] =null;
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    While this might work, It's far better to add details explaining you answer. You can find more tips here: [answer] – ItamarG3 Jun 27 '17 at 06:23
  • 1
    @ItamarGreen is right and it seems that u are copying my answer.Because I have already given the same solution in detail – Frost_Mourne Jun 27 '17 at 06:36
  • 1
    @Frost_Mourne I think you're the first person I see with 101 rep that's **not** from assoc. bonus XD – ItamarG3 Jun 27 '17 at 06:37