900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 为自定义控件添加页面响应事件

为自定义控件添加页面响应事件

时间:2024-04-26 13:46:55

相关推荐

为自定义控件添加页面响应事件

ascx:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

ascx.cs:

(1)

public delegate void PostBackDelegate(); //定义委托类型

public event PostBackDelegate PostBackEvent; //定义委托对象

protected void Button1_Click(object sender, EventArgs e)

{

if (this.PostBackEvent != null) this.PostBackEvent(); //无参数

}

(2)

public event EventHandler PostBackHandler;

protected void Button1_Click(object sender, EventArgs e)

{

if (this.PostBackHandler != null) this.PostBackHandler(sender, e); //有参数

}

aspx.cs:

在Page_Load里(不是!IsPostBack里)加入:

(1)

MyUserControl1.PostBackEvent += new Test_MyFilePath_MyUserControl.PostBackDelegate(MyFuncName); //new自动生成

protected void MyFuncName()

{

Response.Write("OOKK");

}

(2)

MyUserControl1.PostBackHandler += new EventHandler(MyFuncName);

protected void MyFuncName(object sender, EventArgs e)

{

Button btn = (Button)sender;

btn.Text = "控件按钮名称"; //改变控件按钮名称

Response.Write("ookk");

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。