0

I have a textbox(txtSearch) and button(search) and one link button. First I enter some name in the textbox and click the Search button(username is getting assigned) and click on the link button.

Now username parameter value is not getting passed from searchbutton_click event to linkbutton_click event? How can i achieve this?

public partial class Users: System.Web.UI.Page
{
     public string username = string.empty;

     protected void Page_Load(object sender, EventArgs e)
     {

     }

     public void SearchButton_Click(object sender, EventArgs e)
     {
         username = txtUser.Text.ToString();
     }

     protected void linkButton_Click(object sender, EventArgs e)
     {
         response.write(username) \\here username is coming as empty string.
     }
}
furkle
  • 5,019
  • 1
  • 15
  • 24
Selva
  • 431
  • 2
  • 7
  • 13

2 Answers2

1

you can use viewstate like below.

public string username
{
    get { return (string)ViewState["username"]; }
    set { ViewState["username"] = value; }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
user1089766
  • 597
  • 6
  • 14
0

You can use Session, like

  public string username 
  { 
     get { return Session["username"] as string; }
     set { Session["username"] = value; }
  }
Max Brodin
  • 3,903
  • 1
  • 14
  • 23
  • It is working for me. Which is the best one to use? Session or Viewstate here? – Selva Oct 28 '14 at 16:50
  • @Selva Viewstate. ViewState will function properly if the page is open in multiple tabs, Session wouldn't. – Servy Oct 28 '14 at 16:50
  • @Selva Viewstate would work only on one page, Session would work on multiple pages, see this [SO question](http://stackoverflow.com/questions/733482/what-is-the-difference-between-sessionstate-and-viewstate) – Max Brodin Oct 28 '14 at 17:08
  • your answer make sense to me. I will use session properties for my issue. Please let me know this is optimized solution for my situation? – Selva Oct 28 '14 at 17:18
  • I don't really know your situation, if you have only one page, where you use `username` you should user `ViewState`, if you need `username` on other pages - use `Session`. – Max Brodin Oct 28 '14 at 17:25