I create a button dynamically and I want when my button click and onclick event fired, a variable pass with this event.
Button btn = new Button();
btn.Click += new EventHandler(Button_Click);
//btn.Click += new EventHandler(Button_Click(32)); <-- I want something like this
divUserUploadedList.Controls.Add(btn);
And this is Onclick event:
protected void Button_Click(object sender, EventArgs e)
{
//I want to access that value here
}
You can do this by using CommandArgument property
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += btn_Click;
btn.CommandArgument = "12"; //<-- you can pass argument like this
divUserUploadedList.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string value = btn.CommandArgument;
}
What you are looking for is probably the CommandArgument property.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument%28v=vs.110%29.aspx
Related
I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.
public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
Button Btn = new Button
{
Content = Display,
Name = "Btn_" + Name
};
Btn.Click += new RoutedEventHandler(ClickEventHandler);
return Btn;
}
void Btn_save_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");
Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour?
Thanks
From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:
Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));
public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
Button b = new Button()
{
Content = display,
Name = $"Btn_{name}"
};
b.Click += new EventHandler(click);
return b;
}
void SomeOnClickEvent(object sender, EventArgs e)
{
}
void SomeOtherOnClickEvent(object sender, EventArgs e)
{
}
I am not entirely sure what you are trying to accomplish with this.
Here is an example of how to create an event at run time.
public void CreateButton()
{
Button Btn = new Button();
Btn.Click += new EventHandler(btn_Clicked);
}
private void btn_Clicked(object sender, EventArgs e)
{
// Your Logic here
}
I have 2 buttons. The first button gets drawn from the code behind. The second button gets drawn when the first button is clicked. This works perfectly. When I add a event when the second button gets clicked this event won't be triggered. As you can see in the code below there is a btnTwo_click function. If I put a breakpoint on this function the program won't even break. Is there a way to trigger this event from the code behind (not using java script)?
Here is the code, I actually use this system in a table. But this simple code has the same problem in the end.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnOne_Click);
form1.Controls.Add(btn);
}
void btnOne_Click(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnTwo_Click);
form1.Controls.Add(btn);
}
void btnTwo_Click(object sender, EventArgs e)
{
Label lblTest = new Label(); //BREAKPOINT
form1.Controls.Add(lblTest);
}
}
You have to add the second button on the Page_Load or Page_Init method of Page life cycle:
e.g.
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += btnOne_Click;
form1.Controls.Add(btn);
ImageButton btn2 = new ImageButton();
btn2.Command += btnTwo_Click;
btn2.Visible = false;
form1.Controls.Add(btn2);
}
void btnOne_Click(object sender, EventArgs e)
{
// Your second button
form1.Controls[2].Visible = true;
}
void btnTwo_Click(object sender, EventArgs e)
{
ImageButton btn2 = (ImageButton)sender;
// Do something
}
Just move this line to Page_Load event and it will work ;)
btn.Command += new CommandEventHandler(btnTwo_Click);
I am developing a windows application in c#. I have created a button inside datagridview using following code
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridViewTrial.Columns.Add(btn);
btn.HeaderText = "Update";
btn.Name = "btn";
btn.Text = "Update";
btn.UseColumnTextForButtonValue = true;
now i want to change the text of btn to "save" when the update button get clicked.Also i want to update my table.I m not getting it.Please help me:(
If im not mistake you should do this
//Here you add event to button
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
btn.Click -= new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click);
}
}
void btn_Click(object sender, EventArgs e)
{
if(sender is button)
((button)sender).Text = "new text";
}
I hope this help
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
// hook or unhook click event here
}
}
I add buttons dynamically.. e.g. Button newButton=new Button();
Now i want each button to be triggered. So i wrote them the following events:
public void response_Click(object sender, EventArgs e)
{
}
public void edit_Click(object sender, EventArgs e)
{
}
public void quote_Click(object sender, EventArgs e)
{
}
Button quote = new Button();
Button reply = new Button();
Button edit = new Button();
quote.ID = "quote";
reply.ID = "reply";
edit.ID = "edit";
How do i trigger them, as soon as the user clicks on the button..will my functions above be triggered? do i need to do the following:
this.Clicked+=quote;
this.Clicked+=reply;
this.Clicked+=edit;
if i do need to do that..where do i put those lines of code?
i use visual studio 1010. asp.net
You can do like..
quote.Click += new EventHandler(quote_Click);
reply.Click += new EventHandler(response_Click);
edit.Click += new EventHandler(edit_Click);
Yes, you would need to register the event of the button and associate it with particular method.
for example.
myButton.Click += new EventHandler(Button_Click);
void Button_Click(object sender, EventArgs e)
{
}
I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}