怎样为一个动态生成的控件加上事件?

langel 2003-10-19 09:07:12
Page_load的时候我动态生成了一些复选框控件,想为这些控件指定一个鼠标点击的事件处理函数(如:点击复选框则调用一个名为Proc_click的函数),该怎么办?
...全文
37 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
binger2003 2003-10-21
  • 打赏
  • 举报
回复
feixiang1234(飞翔)
俺是asp.net菜鸟,能否给我指点一点点。QQ:54805447
guoyan19811021 2003-10-21
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/Read_Article.asp?Id=21615
langel 2003-10-21
  • 打赏
  • 举报
回复
up
langel 2003-10-20
  • 打赏
  • 举报
回复
非常感谢飞翔的答复。
能不能谈一下Proc_click.Attributes.Add("onclick","??");的用法?
feixiang1234 2003-10-19
  • 打赏
  • 举报
回复
我刚做过,代码如下:
vote.aspx
<%@ Page language="c#" Codebehind="vote.aspx.cs" AutoEventWireup="false" Inherits="vote.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<title>专利排行榜</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css/vote.css" type="text/css" rel="stylesheet">
</HEAD>
<body text="#000000" bgColor="#88d9ff" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
<form id="form1" name="form1" action="" method="post" runat="server">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="185" align="center" border="0"
height="154">
<TR>
<TD align="center" height="40"><asp:panel id="Panel2" runat="server" Height="20px" Font-Bold="True"></asp:panel></TD>
</TR>
<TR>
<TD height="88"><asp:panel id="Panel1" runat="server" Height="86px" BorderStyle="None"></asp:panel></TD>
</TR>
<TR>
<TD height="26"><asp:button id="Button1" runat="server" Text="投票" Width="55px" Height="23px"></asp:button> 
<asp:hyperlink id="HyperLink1" runat="server" Height="14px" NavigateUrl="http://www.vn.sinoviet.com/sv_otherwork/poll/default.asp"
Target="_blank">详细内容>></asp:hyperlink></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>

vote.aspx.cs
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;
using System.Data.SqlClient;

namespace vote
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected static string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.HtmlControls.HtmlForm form1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
ArrayList list=new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
SqlCommand SqlComm=new SqlCommand();
System.Data.SqlClient.SqlConnection SqlConn=new System.Data.SqlClient.SqlConnection(ConnectionString);
SqlConn.Open();
SqlComm.Connection=SqlConn;
SqlComm.CommandText="select * from question order by Q_ID";
SqlDataReader MyReader=SqlComm.ExecuteReader();
MyReader.Read();

System.Web.UI.WebControls.Label lxj=new Label();
lxj.Text=MyReader["Q_Title"].ToString()+"<br>请选择三个专利进行投票:<br>";
this.Panel2.Controls.Add(lxj);

SqlComm.CommandText="select * from answer where Q_ID="+MyReader["Q_ID"].ToString();
MyReader.Close();
SqlDataAdapter sda=new SqlDataAdapter(SqlComm);
DataSet ds=new DataSet();
sda.Fill(ds);
for(int i=0,j=1;i<ds.Tables[0].Rows.Count;i++,j++)
{
System.Web.UI.WebControls.CheckBox chk=new CheckBox();
chk.ID=ds.Tables[0].Rows[i]["A_ID"].ToString();
chk.Text=ds.Tables[0].Rows[i]["A_Answer"].ToString()+"<br>";
this.Panel1.Controls.Add(chk);
chk.Visible=true;
list.Add(chk);
}
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
SqlCommand SqlComm=new SqlCommand();
System.Data.SqlClient.SqlConnection SqlConn=new System.Data.SqlClient.SqlConnection(ConnectionString);
SqlConn.Open();
SqlComm.Connection=SqlConn;
int k=0;
for(int i=0;i<list.Count;i++)
{
if(((CheckBox)list[i]).Checked==true)
k++;
}
if(k==3)
{
for(int i=0;i<list.Count;i++)
{
if(((CheckBox)list[i]).Checked==true)
{
SqlComm.CommandText="update Answer set Q_Vote = Q_Vote+1 where A_ID="+((CheckBox)list[i]).ID.ToString();
SqlDataAdapter sda=new SqlDataAdapter(SqlComm);
DataSet ds=new DataSet();
sda.Fill(ds);
ds.Clear();
sda.Dispose();
}
}
Response.Write("<script>alert(\"投票成功!!!\");history.back();</"+"script>");
Response.End();
}
else
{
Response.Write("<script>alert(\"请选择三个专利进行投票!!!\");history.back();</"+"script>");
Response.End();
}
}
}
}
langel 2003-10-19
  • 打赏
  • 举报
回复
flyinglz(云溪飘叶) 能不能说得具体一点?我不太明白你说的加入服务端函数的方法。
另外 freecs(北狼)说的是否应该是:复选框ID.Attributes.Add("onclick","??");
如果是Proc_click.Attributes.Add("onclick","??");的话为什么??的位置还要写函数名呢?
flyhorse1980 2003-10-19
  • 打赏
  • 举报
回复
学习一下
flyinglz 2003-10-19
  • 打赏
  • 举报
回复
javascript函数

如果要使用服务端函数,试试在加入前先定义事件,或加入后用findcontrl函数找到控件现定义事件
langel 2003-10-19
  • 打赏
  • 举报
回复
??位置是什么东西? 复选框的ID吗?
树猫 2003-10-19
  • 打赏
  • 举报
回复
Proc_click.Attributes.Add("onclick","??");

62,040

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧