I have ASP.NET page with one button visible.
When user clicks it - it instances MyClass (its implementation is in AppCode directory) and turns invisible (button2 becomes visible).
When I click button2 it raises "Object reference not set to an instance of an object" Exception.
What seems to be the problem?
{
public MyClass noviTest;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
noviTest = new MyClass(TextBox1.Text);
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text=noviTest.getID; //this is the critical line
}
}
Since on postback the noviTest reference to MyClass is not recreated.
You can add it to the viewstate to keep a reference to it. (Assuming MyClass is serializable).
In Button1_Click:
ViewState("noviTest") = noviTest;
Then in Button2_Click:
var noviTest = ViewState("noviTest");