<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultipleDynamicButtons.aspx.cs" Inherits="WebUI.Forums.MultipleDynamicButtons" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="place" /><br />
<asp:Literal runat="server" ID="lit" />
</div>
</form>
</body>
</html>
.CS Page
using System;
using System.Web.UI.WebControls;
namespace WebUI.Forums
{
public partial class MultipleDynamicButtons : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
Button btn = new Button();
btn.ID = "Button" + i.ToString();
btn.Text = "Test button" + i.ToString();
btn.Click += new EventHandler(btn_Click);
place.Controls.Add(btn);
}
}
void btn_Click(object sender, EventArgs e)
{
lit.Text = string.Format("Button {0} was pressed.", ((Button)sender).ID);
}
}
}
No comments:
Post a Comment