In C# i would like to access textbox from page directly without sending it as variable to the class for example
file class.cs code
public class A {
private string dosomething {
string text;
text = textbox1.text;
// textbox1 exists in, for example, default.aspx, and I need it's
// value in the class after some event occurred - let's say there
// is button and it was clicked
return text;
}
}
default.aspx.cs code
protected void Button1_Click(object sender, EventArgs e) {
A a = new A();
// I need when this button clicked to fill the variable within
// the class with the data given from the textbox within this page
}
This is what I’ve come up with, but I’m not sure if I’m taking the right path using a getter and setter this way:
private TextBox TextBox1 = new TextBox();
public string settext {
get { return TextBox1.Text; }
set { TextBox1.Text = value;}
}
but I always get a NullReferenceException was unhandled message.
i found what i’m looking for and it’s working like charm i will put the way i did and the link for the article helped me to figure out how to solve it
http://codebetter.com/jefferypalermo/2004/09/01/asp-net-2-0-master-pages-changes-the-pages-control-hierarchy-level-300/
in case using master page same idea for without master page in my case i was using master page and i tested it on both with and without working fine