asp.net dynamically button with event handler
You must have to place that code in page_load
or page_init
event.
protected void Page_Load()
{
Button ButtonChange = new Button();
ButtonChange.Text = "Change";
ButtonChange.ID = "change_" + i.ToString();
ButtonChange.Font.Size = FontUnit.Point(7);
ButtonChange.ControlStyle.CssClass = "button";
ButtonChange.Click += new EventHandler(test);
}
Read MSDN article - How to: Add Controls to an ASP.NET Web Page Programmatically?
adding event handler to a dynamically created button
protected void Page_Load(object sender, EventArgs e)
{
Button dodaj = new Button();
dodaj.Text = "Click Me!";
dodaj.Attributes.Add("runat", "server");
dodaj.Attributes.Add("type", "submit");
dodaj.Attributes.Add("onclick", "addKosarica");
newDivInfo.Controls.Add(dodaj);
dodaj.Click += Dodaj_Click1;
}
private void Dodaj_Click1(object sender, EventArgs e)
{
Response.Write("Ok");
}
aspx code
<div runat="server" id="newDivInfo">
C# ASP - Dynamic button creation and assigning event handlers
Here's what happens.
- Someone clicks Button1. This causes a PostBack
- ASP.Net receives the POST request, creates a new instance (that's key) of your Page class, and begins to re-run the entire Page life cycle.
- The
Button1_Click()
code runs as part of the page life cycle and puts new buttons into the Page class - The Page class renders HTML to the Response, where the browser receives it and renders it to the screen for your users. At this point, the Page class instance you've been working with on the server — the one with all the new buttons — is destroyed. The buttons were rendered to the browser, though.
- A user clicks on one of your new buttons. This causes a new PostBack.
- ASP.Net receives the POST request and creates a new instance of the Page class.
This is where things get messy. That new instance of the Page class doesn't have any dynamic buttons.
You need code to re-add those button into the class for every PostBack. Additionally, event handlers for the ASP.Net page life are determined before the Page_Load event. So if you wait until Page_Load to setup the dynamic buttons, things won't work right. You'll see them on the screen. You can click them and force a PostBack (because the event handler will be there for the Render step later on). But nothing will happen then you click the button.
To fix this, the buttons need to be (re)created in Page_Init or Page_PreInit.
How do add an EventHandler for a dynamically generated Button
Create your buttons with an ID before you bind the event:
Button btn1 = new Button();
btn1.ID = "btnMyButton";
btn1.Click += new EventHandler(this.btn1_Click);
Make sure every button has an unique ID. Also, I would personally move the code to Page_Init
instead of Page_Load
.
C# - Event handler for dynamic buttons
Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists.
For quick demo see this code
ASPX CODE
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>
ASPX.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
generate();
}
}
public void generate()
{
if (!pnl.HasControls())
{
for (int i = 0; i < 4; i++)
{
Button detailsBtn = new Button();
detailsBtn.Text = "fName" + i.ToString();
detailsBtn.ID = i.ToString();
detailsBtn.Click += new EventHandler(detailsBtn_Click);
pnl.Controls.Add(detailsBtn);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
generate();
}
protected void detailsBtn_Click(object sender, EventArgs e)
{
}
Add click event handler to a button that was dynamically created in another class
You have assumed correct. Don't create a new instance of _Default class.
ASPX:
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
make me invisible;
</asp:Panel>
</div>
</form>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
GenerateButtons();
}
public void GenerateButtons()
{
AnotherClass anotherClass = new AnotherClass(this);
}
public void btnConfirmBook_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
DisplayConfirmation();
}
protected void DisplayConfirmation()
{
Panel1.Visible = false;
}
Another Class:
public class AnotherClass
{
public AnotherClass(Default def)
{
Button btnConfirm = new Button();
btnConfirm.Text = "Confirm";
btnConfirm.CommandName = "Variable1,Variable2,Variable3";
def.Form.Controls.Add(btnConfirm);
btnConfirm.Click += new EventHandler(def.btnConfirmBook_Click);
}
}
Related Topics
How to Check If a Number Is Positive or Negative in C#
ASP.NET Identity Change Password
Blocking Access to Private Member Variables? Force Use of Public Properties
Importing Excel into a Datatable Quickly
C# Code for Association, Aggregation, Composition
Proper Datagrid Search from Textbox in Wpf Using Mvvm
How to to Return an Image with Web API Get Method
Auto-Resize Multiple Forms Rendered on Panel
Trim Last Character from a String
Insert Datetime Value in SQL Database with C#
How to Fill Part of Image with Color
Get the Sourcecontrol of a Dropdownmenu
Writing to Output Window of Visual Studio
Determine Operating System in .Net Core
How to Use C# 6 with Web Site Project Type
Ms Chart Rectangular Annotation Width in Percent and Not Pixel