G
gnewsgroup
Suppose I create a button on the fly and assign an event handler to it
in the code-behind like so:
protected Button CreateMyButton()
{
Button myButton = new Button();
myButton.Text = "Test";
myButton.Click += new EventHandler(myButton_Click);
return myButton;
}
Now, in myButton_Click event handler, how do I get a reference to the
newly created Button object called myButton?
In the Click event handler, we cannot directly say something like
myButton.Text = "Text has changed";
which I tried, and for which I got this:
System.NullReferenceException: Object reference not set to an instance
of an object.
I got the same exception even if I declared myButton as a class-level
private member.
So here is the question: How to refer to a dynamically created control
in the event handler?
Thank you.
in the code-behind like so:
protected Button CreateMyButton()
{
Button myButton = new Button();
myButton.Text = "Test";
myButton.Click += new EventHandler(myButton_Click);
return myButton;
}
Now, in myButton_Click event handler, how do I get a reference to the
newly created Button object called myButton?
In the Click event handler, we cannot directly say something like
myButton.Text = "Text has changed";
which I tried, and for which I got this:
System.NullReferenceException: Object reference not set to an instance
of an object.
I got the same exception even if I declared myButton as a class-level
private member.
So here is the question: How to refer to a dynamically created control
in the event handler?
Thank you.