so, I have a form that is dynamically populated with textboxes and buttons.
How can I create EventHandlers for each of those buttons dynamically (ex: it generates 20 buttons, I need 20 eventhandlers). Each button will have the same function (to delete something from a database) but I need the program to know whenever any one of them is clicked to trigger that code.
// also, the button creation code is within a while() so I can't use it ouside that while (just pointing that out)
Code:
public void LoadElements()
{
//more code here
while(some condition)
{
// more code above
Button b = new Button();
b.Text = "Delete";
b.Name = "button" + j;
b.Location = new Point(240, Y);
Controls.Add(b);
// more code bellow
}
// more code here
}
Assign them like you would for any other event in your code. You can simply add an event handler doing something like:
b.Click += b_Click
Add in the loop:
b.Click+=New Eventhandler(b_Click);
(Just press TAB twice after typing b.Click+=).
Define the function b_Click outside of the loop. It will be invoked when anyone of those button is clicked.
Related
I'm looping a collection of strings, coming from a database; for each entry, I create a new Button which is then added to a FlowLayoutPanel.
The Text of each Button is set to the current item in the string collection.
I'd like to assign an EventHandler to the Click event of each Button, however I am only able to access the Properties of a Button.
I have to cast the last entry of the FlowLayoutPanel's Controls collection to Button and add the Event Handler to this instance.
Does anyone know the reason why I can't access anything else then Properties? Is there a cleaner way of coding that?
List<string> temp = Database.GetNames();
foreach(string s in temp)
{
flp_main.Controls.Add(new Button()
{
Text = s.name
});
Button b = (Button)flp_main.Controls[flp_main.Controls.Count - 1];
b.Click += B_Click;
}
Asking for Improvements on Code Quality
I want to add buttons from a list, depending of how many items there are in the list. It works perfectly when I do it like this:
The thing is they have no click events, I want each button to have an event that makes the user navigate to the right page depending on which button is clicked.
I'm trying to do it this way but it doesn't work:
Any ideas of the right way to do it if this is totally wrong?
Create the button before adding in to your StackLayout:
foreach(var item in question.Answers)
{
var button = new Button();
button.Text = item.AnswerText;
button.Clicked += async delegate { await Navigation.PushAsync(item.NextPage); };
stack.Children.Add(button);
}
You can try with this.
foreach(var item in question.Answers){
var button = new Button{Text=item.AnswerText};
button.Clicked += async(s,e)=> await Navigation.PushAsync(item.NextPage);
stack.Children.Add(button);
}
I've create a dashboard that contains multiple dynamic buttons. When I click the "Delete" button I want the server side code to execute. I've added the code to the Click event handler of my button.
The dynamic button is created with the following code:
Button PopupDeleteButton = new Button();
PopupDeleteButton.ID = "PBD" + controlValue.ID.ToString();
PopupDeleteButton.Text = "Delete";
PopupDeleteButton.Style["Height"] = "15px";
PopupDeleteButton.Style["Width"] = "50px";
PopupDeleteButton.Style["top"] = "0";
PopupDeleteButton.Style["right"] = "0";
PopupDeleteButton.Style["float"] = "left";
PopupDeleteButton.Style["font-size"] = "9px";
PopupDeleteButton.Style.Add(HtmlTextWriterStyle.VerticalAlign, "top");
PopupDeleteButton.Attributes.Add("runat","server");
PopupDeleteButton.Click += new EventHandler(this.RemoveWidgetFromXML_Click);
However when the I click the dynamic button on the form a post back occurs, and the function RemoveWidgetFromXML is never called. Any ideas? I've looked all over Google but can't find how to stop the post-back from occurring prior to the event call.
You have to recreate dynamic control on every page postback. In that case event handler will be called.
I want to create dynamic buttons on button click event(for example., btnCreateDynamic_Click).
I tried creating dynamic buttons on page_load event and Pre_int event.They are all working but i want to create them in button click event. How can i do this in c# asp.net?
Your button click event at the client will cause a page postback that will start the ASP.Net Page Life-cycle;
Your button click event on the server is a PostBackEvent and you should be able to use the same method call CreateMyButton() that you used in the Load or Init events.
An idea would be to create a list of buttons in which you'd store the buttons you created in btnCreateDynamic_click.
you could have a method like:
private Button CreateButton(string id, string name)
{
Button b = new Button();
b.Text = name;
b.ID = id;
b.Click += new EventHandler(Button_Click);
b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
return b;
}
in btnCreateDynamic_click you could have something like:
Button b = CreateButton("dinamicBtn"+myDinamicButtonsList.Count.ToString(),"dinamicBtn"+myDinamicButtonsList.Count.ToString());
myDinamicButtonsList.add(b);
and in the pageLoad for example you could do something like
foreach(button btn in myDinamicButtonsList){
form1.Controls.Add(btn));
}
List<Button> myDinamicButtonsList = new List<Button>();
myDinamicButtonsList should be stored somewhere from where it could be retrieved after each request.
EDIT:
In page load you could have something like this:
if(Session["myDinamicButtons"] == null){
List<Button> myDinamicButtonsList = new List<Button>();
Session["myDinamicButtons"] = myDinamicButtonsList;
}
foreach(Button btn in Session["myDinamicButtons"] as List<Button>){
form1.Controls.Add(btn));
}
i didn't tested it but it should work.
I want to create buttons or list of items on the basis of number of items in my database or from list of items in my array and for each item create a onclick function for either buttons or any list of items
How about:
int y = 10;
foreach (string name in names)
{
Button button = new Button();
button.Text = name;
button.Position = new Point(10, y);
y += 20;
button.Click += HandleButtonClick;
Controls.Add(button);
}
You might also store the buttons in an array or a list... there's nothing particularly special about GUI controls that stops you from creating them at execution time just like any other object.
If that doesn't help, please give more information about what you need to do that the above doesn't help you with.
I have done it also by looking at Visual Studio code.