請大家幫我看下為什麼事件沒反應
protected void Button1_Click(object sender, EventArgs e)
{
//创建一个按钮
Control c = ParseControl("<asp:Button Text = '按我' ID = 'myButton' runat = 'server' />");
this.GridView1.Controls.Add(c); //创建一个按钮事件
Button myButton = (Button)GridView1.FindControl("myButton");
myButton.Command += new CommandEventHandler(this.myButton_Click);//在此處不能調用事件
} public void myButton_Click(Object sender, EventArgs e)
{
...
}
protected void Button1_Click(object sender, EventArgs e)
{
//创建一个按钮
Control c = ParseControl("<asp:Button Text = '按我' ID = 'myButton' runat = 'server' />");
this.GridView1.Controls.Add(c); //创建一个按钮事件
Button myButton = (Button)GridView1.FindControl("myButton");
myButton.Command += new CommandEventHandler(this.myButton_Click);//在此處不能調用事件
} public void myButton_Click(Object sender, EventArgs e)
{
...
}
解决方案 »
- DataTable dt = GetDataSource();
- 今天更新了系统补丁,应用程序出现问题
- 一個關於在後台如何獲得前台的控件的名字以及屬性的問題
- 请教关于DropDownList数据邦定的问题!!
- 请问如何学习水晶报表?我没学过,有什么入门的书或者网站吗?
- ASP内存溢出,这是以前别人写的。*这个通配符怎么都看不懂。
- 请教一个关于DataList的问题,谢谢。
- 那里有  等 编码对照表,我想用编码代替 符号 ' ,因为不想他出现在我形成的SQL语句中
- 哪位高手帮我解释两个个控件,我有些看不明白
- 截取字符串时如果,字符串长度不够10就会出错,有办法解决吗?
- 这个正则怎么写?
- 字符串截取问题!!!!!!!!!!!!
页面的page指令<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxxxx.aspx.cs" Inherits="xxxxx_xxx_xxxx" %>如果是在页面上放了个 <asp:button id="button1" /> 那么在提交到服务器上的时候,根据AutoEventWireup 这个属性 ,
asp.net 可以把 ID为button1的按钮和 button1_Click 事件处理程序关联,而你的代码里,当点击了Button1按钮,执行Button1_Click函数,在这个函数里动态生成一个按钮,然后进行事件绑定,但是如何触发它的事件呢?
页面的生命周期很重要。
Button myButton = new Button()
myButton.Text ="按我"
myButton.ID = "myButton"
this.GridView1.Controls.Add(myButton); //创建一个按钮事件
myButton.Command += new CommandEventHandler(this.myButton_Click);//在此處不能調用事件
{
button1.Command += new CommandEventHandler(myButton_Click);
} public void myButton_Click(Object sender, CommandEventArgs e)
{
Response.Write("asdsad");
}
要相触发
要是页面的 PreOnload 之前才行吧
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;public partial class Default2 : System.Web.UI.Page
{ protected override void OnPreInit(EventArgs e)
{
button1.Click += new EventHandler(myButton_Click);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
button1.Visible = false;
}
} public void myButton_Click(Object sender, EventArgs e)
{
Response.Write("asdsad");
}
protected void DataBound(object sender, RepeaterItemEventArgs e)
{
e.Item.FindControl("delPic");
}
protected void Button2_Click(object sender, EventArgs e)
{
button1.Visible = true;
}
}
改成
myButton.Click += new EventHandler(myButton_Click);