动态创建多个按钮,如何编写onclick事件,要求:点击不同按钮产生不同事件

解决方案 »

  1.   

    Button b = new Button();
    b.Click += new  ...//绑定不同的事件处理函数.
      

  2.   

    AddHandler btn.Click, AddressOf boolColumn_OnClickedprivate sub boolColumn_OnClicked(ByVal sender As Object, ByVal e As EventArgs)
            'event
    end sub对每个BUTTON加上这个不知道行不行
      

  3.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace CSWeb
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

    Button[] ButtonArray = new Button[2];
    private void Page_Load(object sender, System.EventArgs e)
    {
    ButtonArray[0] = new Button();
    ButtonArray[0].Text = "Button1";
    this.PlaceHolder1.Controls.Add(ButtonArray[0]);
    ButtonArray[0].Click += new System.EventHandler(this.Button1_Click);
    ButtonArray[1] = new Button();
    ButtonArray[1].Text = "Button2";
    this.PlaceHolder1.Controls.Add(ButtonArray[1]);
    ButtonArray[1].Click += new System.EventHandler(this.Button2_Click);
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    this.TextBox1.Text = "Button1 Clicked!";
    }
    private void Button2_Click(object sender, System.EventArgs e)
    {
    this.TextBox1.Text = "Button2 Clicked!";
    } }
    }
      

  4.   

    我可能没有说明白,现在补充一下:
    动态创建的button数量不定
      

  5.   

    也可以,在同一个事件里判断出来是哪个按钮点击就可以,将不同按钮text设置成不同,messagebox出来,麻烦你用vb.net写一下,多谢!!
      

  6.   

    把事件关联到一个方法里. 用sender区分 .
    Button[] ButtonArray = new Button[N];
    for (int i=0;i<N;i++)
    {
    ButtonArray[i]=new Button();
    ButtonArray[i].Click+=new System.EventHandler(this.Button1_Click);
    }
    事件里.
    private void Button1_Click(object sender, System.EventArgs e)
    {
    Button b=(Button)sender;
    ....// }
      

  7.   

    看明白了,但是如何用vb.net写出来?
    Button b=(Button)sender;
      

  8.   

    网页中动态定义控件
    Page_load中
    Dim BR As Literal
    Dim nchk As CheckBox
    Dim nBut As Button
    If nTabReg.Rows.Count > 0 Then
        Dim nCount As Integer
        For nCount = 0 To nTabReg.Rows.Count - 1
        BR = New Literal
        BR.Text = "<tr>" & vbCrLf
        BR.Text = "<td bgcolor='#00FFCC' align=center>"
        Me.PlaceHolder1.Controls.Add(BR)    nchk = New CheckBox
        nchk.ID = nTabReg.Rows(nCount).Item("Reg_ID").ToString
        nchk.Text = ""
        nchk.Checked = False
        Me.PlaceHolder1.Controls.Add(nchk)    BR = New Literal
        BR.Text = "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFFF' align=center>" & nTabReg.Rows(nCount).Item("Reg_Name") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFCC' align=center>" & nTabReg.Rows(nCount).Item("Reg_Code") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFFF' align=center>" & nTabReg.Rows(nCount).Item("Com_Dep") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFCC' align=center>" & IIf(nTabReg.Rows(nCount).Item("Reg_leader") = 1, "领导", "员工") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFFF' align=center>" & nTabReg.Rows(nCount).Item("Reg_MobileTelephone") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFCC' align=center>" & nTabReg.Rows(nCount).Item("Reg_order") & "</td>" & vbCrLf
        BR.Text = BR.Text & "<td bgcolor='#00FFFF' align=center>"    Me.PlaceHolder1.Controls.Add(BR)
        nBut = New Button
        nBut.Text = "编辑"
        nBut.ID = "B" & nTabReg.Rows(nCount).Item("Reg_ID").ToString
        AddHandler nBut.Click, AddressOf nBut_Click
        Me.PlaceHolder1.Controls.Add(nBut)    BR = New Literal
        BR.Text = "</td>" & vbCrLf
        BR.Text = BR.Text & "</tr>" & vbCrLf
        Me.PlaceHolder1.Controls.Add(BR)
        Next
    End If
    增加过程
        Sub nBut_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim nPostID As String
            nPostID = sender.id.ToString
            '  Response.Write(nPostID)
            If nPostID.Chars(0) = "B" Then
                nPostID = nPostID.Substring(1)
                Dim strScript As String
                strScript = "<body onload = OpenWinAddNew(" & nPostID & ")></body>"
                Response.Write(strScript)
            End If
    End Sub
      

  9.   

    我从vb.net转C#知道vb怎样控制,却不知道C#如何写
       Dim nPostID As String
       nPostID = sender.id.ToString
       Response.Write(nPostID)
    有哪位知道告诉一下好吗?
      

  10.   

    这是C#的操作方法
                               private void nBut_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    string myID;
    ImageButton myBut;
    myBut=(ImageButton)sender;

    myID=myBut.ID.ToString();
    myID=myID.Replace("B_","");
    Response.Redirect("WebEdShop.aspx?ID=" + myID); }
      

  11.   

    AddHandler btn.Click, AddressOf boolColumn_OnClicked
      

  12.   

    http://community.csdn.net/Expert/topic/4302/4302272.xml?temp=.7735865
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Miner
    {
        /// <summary>
        /// Summary des cription for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
    private System.Windows.Forms.Panel panel1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    ///  private Button[] n =new Button[100];
    private int[] kn=new int[100]; private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.panel1 = new System.Windows.Forms.Panel();
    this.SuspendLayout();
    // 
    // panel1
    // 
    this.panel1.Location = new System.Drawing.Point(8, 8);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(400, 400);
    this.panel1.TabIndex = 0;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(416, 413);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                          this.panel1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    int a=0;
    int x=0,y=0;
    for (a=0;a<=99;a++)
    {
    n[a] = new Button();
    n[a].BackColor =Color.White;
    n[a].FlatStyle = FlatStyle.Flat;
    n[a].Width = panel1.Width / 10;
    n[a].Left = x * n[a].Width;
    n[a].Height = panel1.Height / 10;
    n[a].Top = y * n[a].Height;
    n[a].Name = "b" + a;
    panel1.Controls.Add(n[a]);
    panel1.Controls[a].MouseDown  += new MouseEventHandler(this.ButtonArray_OnClick);
    x += 1;
    if (x == 10)
    {
    x = 0;
    y += 1;
    }
    }
    } private void ButtonArray_OnClick(object sender, MouseEventArgs e)
    {
    MouseEventArgs arg=(MouseEventArgs)e;
    Button b1=(Button)sender;
    if (arg.Button==MouseButtons.Right )
    b1.BackColor=Color.White ;
    else
    {
    //b1.BackColor =Color.White ;
    b1.Image=Image.FromFile("f:\\my documents\\my pictures\\elements\\regular_smile.gif");
    }         
    }
        }
    }
      

  13.   

    Button a=new Button();
      a.Attributes.Add("onclick","show();");
      public void show()
     {
      string script="<script language=javascript>window.alert('a')</script>";
      Page.RegisterStartupScript("xxx",script);
      return;
    }