1 LinkButton label;
2 for (int i = 0; i < ((seat.Rows.Count % 10) == 0 ? (seat.Rows.Count / 10) : (seat.Rows.Count / 10) + 1); i++)
3 {
4 for (int j = 0; j < 10; j++)
5 {
6 label = new LinkButton();
7 if (int.Parse(seat.Rows[(i * 10 + j)]["seatstate"].ToString()) == 1)
8 {
9 label.BackColor = Color.Green;
10 }
11 else
12 {
13 label.BackColor = Color.Red;
14 }
15 label.Width = 60;
16 label.Height = 25;
17 label.ID = "lbl" + (j + 1).ToString() + "_" + (i + 1).ToString();
18 label.Text = (j + 1).ToString() + "-" + (i + 1).ToString();
19 label.Style["Position"] = "Absolute";
20 label.Style["Top"] = i * 25 + 25 + "px";
21 label.Style["Left"] = j * 60 + 60 + "px";
22 label.Visible = true;
23 label.Click += new EventHandler(lblSeat_Click);
24 penSeat.Controls.Add(label);
25
26 }
27
28
29 public void lblSeat_Click(object sender, EventArgs e)
30 {
31 this.Page.ClientScript.RegisterStartupScript(GetType(),"","window.alert('ok');",true);
32 }
我已经测试
后台:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button label = new Button();
label.Width = 60;
label.Height = 25;
label.Visible = true;
label.Click += new EventHandler(lblSeat_Click);
form1.Controls.Add(label);
label = new Button();
label.Width = 60;
label.Height = 25;
label.Visible = true;
label.Click += new EventHandler(lblSeat_Click);
form1.Controls.Add(label);
}
public void lblSeat_Click(object sender, EventArgs e)
{
Random n = new Random();
Response.Write(n.Next().ToString());
}
}
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>
</div>
</form>
</body>
</html>
测试结果是正确的。
能否把问题说的具体点,没有理解您的意思?
先动态添加控件,再为这些控件,。台注册页面脚本就可以满足你的要求了,
label.Click += new EventHandler(lblSeat_Click);
注册事件的名称是不一样的,不一样应该就可以写不同的事件处理器了
显然楼主没有描述清楚问题。
for (int i = 0; i < ((seat.Rows.Count % 10) == 0 ? (seat.Rows.Count / 10) : (seat.Rows.Count / 10) + 1); i++)
3 {
4 for (int j = 0; j < 10; j++)
5 {
6 label = new LinkButton();
7 if (int.Parse(seat.Rows[(i * 10 + j)]["seatstate"].ToString()) == 1)
8 {
9 label.BackColor = Color.Green;
10 }
11 else
12 {
13 label.BackColor = Color.Red;
14 }
15 label.Width = 60;
16 label.Height = 25;
17 label.ID = "lbl" + (j + 1).ToString() + "_" + (i + 1).ToString();
18 label.Text = (j + 1).ToString() + "-" + (i + 1).ToString();
19 label.Style["Position"] = "Absolute";
20 label.Style["Top"] = i * 25 + 25 + "px";
21 label.Style["Left"] = j * 60 + 60 + "px";
22 label.Visible = true;
23 label.Click += new EventHandler(lblSeat_Click);
24 penSeat.Controls.Add(label);
25
26 }
上面的代码应该放在PageLoad或PageInit里。
可怜的楼主被微软的这种开放捆绑了......
还有服务端控件.....灵活性太差了.....