Reputation: 134
Add Buttons:
Button button = new Button();
this.Controls.Add(button);
button.Name = "btn" + id;
button.Text = "AAAA";
The result is the name of a button as btn55
, but, how to change text on button name btn55
?
something like
private void btnaktual_Click(object sender, EventArgs e)
{
btn55.Text = "BBB";
}
Gives me error:
Error 1 The name 'btn55' does not exist in the current context
Upvotes: 2
Views: 6814
Reputation: 148
You need to search for the button in it's parent controller. Let's say you have a main form and the button placed in it. You can find using id of control in parent.
(MainForm.Controls.Find("btn"+number") as Button).Text= "BBB";
Upvotes: -1
Reputation: 3469
Your entire problem lies in the page load. When you click the button event the page posts back, so if you're control is not reloaded it no longer exists when the click event is called. Either you are not creating the button on postback or the button isn't in viewstate. Find Control will always return null until the button is created so most of these answers won't work for you yet.
These pages will help you:
Dynamically Created Controls losing data after postback
http://www.codeproject.com/Articles/35360/ViewState-in-Dynamic-Control
Once you get that control reloaded, then you can get it from there pretty easily.
Upvotes: 0
Reputation: 700162
If you used the designer to put the button in the form, then a member variable would be created with the same name as the name of the form. As you create the button using code, there is no btn55
variable.
When you create the button you have a reference to it. You should just keep that reference so that you can use it later. Make a member variable in the form where you can store it, i.e. declare Button button;
(or perhaps a more descriptive name) in the form class instead of inside the method where you create the button. Then you can use the variable later to access the button.
Upvotes: 1
Reputation: 1006
Use this.Controls.Find("btn55",true).FirstOrDefault().Text = "BBB";
Upvotes: 1