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);
}
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 am dynamically creating a RadMenu. I want to use the menu click event of the RadMenu.
Actually I am using if(!Scriptmanager.IsInAsyncPostBack) condition.
Within the if condition I only write code to create the menu dynamically. I'll give the sample code for better understanding.
if (!src.IsInAsyncPostBack)
{
RadMenu menu = new RadMenu();
RadMenuItem item1 = new RadMenuItem();
item1.Text = "Home";
RadMenuItem item11 = new RadMenuItem();
item11.Text = "Home";
item1.Items.Add(item11);
RadMenuItem item2 = new RadMenuItem();
item2.Text = "About";
RadMenuItem item3 = new RadMenuItem();
item3.Text = "Contact";
menu.ItemClick += new RadMenuEventHandler(menu_ItemClick);
menu.Items.Add(item1);
menu.Items.Add(item2);
menu.Items.Add(item3);
Page.Controls.Add(menu);
}
void menu_ItemClick(object sender, RadMenuEventArgs e)
{
Response.Redirect("Home.aspx");
}
When page loading if(!IsInAsyncPostBack) condition true so the RadMenu is created dynamically and loaded in the page.
When I click the menu item, Postback becomes true, so the if condition fails and the menu
click event is not firing.
I wrote the menu click event outside of the if condition. In this case the menu click event is also not firing.
My exact requirement is that I want to use if(!IsInAsyncPostBack) postback condition as well as I need to use menu click event. Is it possible?
Add this in the Page_Init event and remove the if (!src.IsInAsyncPostBack) check. THis is the easiest way to create controls programmatically.
Probably your menu is ajax-enabled so a POST from it is partial, so your code does not recreate it, so it cannot raise its handler.
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.
I read this topic (Adding buttons to a TabControl Tab in C#) but I don't figure out why my code below add one button only to the tabpage.
I've obviously debugged that the foreach works properly.
foreach (string line in File.ReadAllLines(#"C:\quicklauncher.ini"))
{
TabPage page = new TabPage(foldername);
DirectoryInfo d = new DirectoryInfo(line);
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button();
button.Text = file.Name;
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
}
tabControl.TabPages.Add(page); //add our tab page to the tab control
}
Thanks,
Steve
You thought it added only 1 button for you but in fact it did not, it added all the buttons for you but those buttons had the same Location (which is (0,0) by default). That's why you did think there was only 1 button (because you saw only 1 last button on top of others).
You added buttons automatically to your tabpage, so you should have some rule to locate them, I'm not sure what that rule is but I suppose you want to line them up vertically (just an example), I'm going to correct your code to achieve such a thing, at least you will see it work, and in fact all the buttons are added normally:
//you need some variable to save the next Top for each new button:
//let's call it nextTop:
int nextTop = 0;
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button { Top = nextTop,
Text = file.Name };
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
nextTop += button.Height + 5; //it's up to you on the
//Height and vertical spacing
}
//...
You can also try using some layout control like FlowLayoutPanel and TableLayoutPanel to contain all the buttons, they can help arrange your buttons in some way you may want, just try it.
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.