Search code examples
c#asp.netencapsulation

How can I pass a string from an onclick event to another class


I am new to encapsulation. I would like to pass a string from the OnClick event of one of my buttons to a private method in another class. When I try to set the string from inside the method now it says that variable isn't recognized How can I safely pass it in?

My OnClick Event:

public partial class ClassOne
  protected void lnkbtnKeySearch_Click(object sender, EventArgs e)
      {
           string myNewString = "Change the value of the string";
      }

Class I want to pass it into:

public partial class ClassTwo
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        {
            string filterText = myNewString;
        }
    }

Solution

  • You can try to hold the value of the string inside a session variable:

    protected void lnkbtnKeySearch_Click(object sender, EventArgs e)
    {
         Session["myNewString"] = "Change the value of the string";
    }
    
    private void LoadData()
    {
         string filterText = Session["myNewString"].ToString();
    }