动态创建多个按钮,如何编写onclick事件,要求:点击不同按钮产生不同事件
解决方案 »
- 网站添加数据时出现问题
- 请教一个入门级的正则
- 高分求助.NET2003的问题
- 遍历过滤数据 存入数组
- 关于web与桌面的相互转换的问题
- DataList绑定数据时,EditItemTemplate模板中使用了FCKEditor控件,怎么获取FCKEditor的Value值?
- 急急!!如何在aspx中嵌套excel,并能从其中提出cell
- Asp.NET使用 HubbleDotNet 做搜索
- C#中,如何动态给DataGrid添加模板列,且控件为DropDownList,并关联到数据库。谢谢了。
- 求教:如何在传统asp中调用c#编写的dll?
- 疑惑:转义字符"@"的作用是什么?
- 怎么在DataGrid的模板列中能用 代码 + Container.Value啊??
b.Click += new ...//绑定不同的事件处理函数.
'event
end sub对每个BUTTON加上这个不知道行不行
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!";
} }
}
动态创建的button数量不定
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;
....// }
Button b=(Button)sender;
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
Dim nPostID As String
nPostID = sender.id.ToString
Response.Write(nPostID)
有哪位知道告诉一下好吗?
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); }
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");
}
}
}
}
a.Attributes.Add("onclick","show();");
public void show()
{
string script="<script language=javascript>window.alert('a')</script>";
Page.RegisterStartupScript("xxx",script);
return;
}