在 C# 中,我想直接从页面访问文本框,而不将其作为变量发送给类,例如
文件 class.cs 代码
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 代码
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
}
这是我想出的,但我不确定我是否以这种方式使用 getter 和 setter 走正确的道路:
private TextBox TextBox1 = new TextBox();
public string settext {
get { return TextBox1.Text; }
set { TextBox1.Text = value;}
}
但我总是收到一条NullReferenceException was unhandled
消息。