首页 新闻 会员 周边 捐助

给控件添加事件

0
悬赏园豆:200 [已解决问题] 解决于 2012-04-18 19:26

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 }

问题补充: 我是想动态在页面生成多个linkbutton 并给他们添加点击事件 但是现在label.Click添加事件后却等于System.EventHander; 点击也不能Write:“1” 这是前台页面生成的代码:10-2
乄蛇的主页 乄蛇 | 初学一级 | 园豆:22
提问于:2011-04-17 10:17
< >
分享
最佳答案
0

我已经测试

后台:

     

 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>

测试结果是正确的。

收获园豆:200
--中庸-- | 菜鸟二级 |园豆:408 | 2011-04-26 21:28
其他回答(5)
0

能否把问题说的具体点,没有理解您的意思?

DYStudio.Net | 园豆:1747 (小虾三级) | 2011-04-17 10:28
0

先动态添加控件,再为这些控件,。台注册页面脚本就可以满足你的要求了,

小小刀 | 园豆:1991 (小虾三级) | 2011-04-17 12:03
0
  label.Click += new EventHandler(lblSeat_Click);

 

注册事件的名称是不一样的,不一样应该就可以写不同的事件处理器了

红萝卜 | 园豆:240 (菜鸟二级) | 2011-04-17 12:10
0

显然楼主没有描述清楚问题。
 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里。

Jeremy Liu | 园豆:705 (小虾三级) | 2011-04-18 10:40
0

可怜的楼主被微软的这种开放捆绑了......

还有服务端控件.....灵活性太差了.....

[秦时明月] | 园豆:738 (小虾三级) | 2011-05-25 15:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册