为自定义控件添加页面响应事件
生活随笔
收集整理的這篇文章主要介紹了
为自定义控件添加页面响应事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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");
}
<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");
}
轉載于:https://www.cnblogs.com/vipcjob/archive/2009/07/30/1534585.html
總結
以上是生活随笔為你收集整理的为自定义控件添加页面响应事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 八八坑道53度多少钱
- 下一篇: ASP.NET 3.5揭秘-读书笔记1