你的想法当然可以想办法实现,但我首先想告诉你的是,这样做极大程度上违背了面向对象编程思想里的封装规则!这样做,会使你的程序变得不可理喻,button1是a.ascx的成员,其事件应该在a.ascx这个类中处理,在b.aspx中处理的只能是a.ascx的事件,因此,你应该在a.ascx创建一个事件,使button1的click事件升级为a.ascx的一个事件.以下示例创建了一个自定义按钮 MyButton,该按钮引起回发、捕获回发事件并在服务器上引发 Click 事件。若要生成该示例,请参阅服务器控件示例中的说明。[C#] using System; using System.Web.UI;namespace CustomControls { public class MyButton: Control, IPostBackEventHandler { // Defines the Click event. public event EventHandler Click;
// Invokes delegates registered with the Click event. protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } }
// Method of IPostBackEventHandler that raises change events. public void RaisePostBackEvent(string eventArgument) { OnClick(EventArgs.Empty); }
protected override void Render(HtmlTextWriter output) { output.Write("<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />"); } } } [Visual Basic] Option Explicit Option StrictImports System Imports System.Web.UINamespace CustomControls Public Class MyButton Inherits Control Implements IPostBackEventHandler ' Defines the Click event. Public Event Click As EventHandler
' Invokes delegates registered with the Click event. Protected Overridable Sub OnClick(e As EventArgs) RaiseEvent Click(Me, e) End Sub
' Method of IPostBackEventHandler that raises change events. Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent OnClick(EventArgs.Empty) End Sub
Protected Overrides Sub Render(output As HtmlTextWriter) output.Write("<INPUT TYPE = submit name = " & Me.UniqueID & _ " Value = 'Click Me' />") End Sub End Class End Namespace 在页上使用控件 下面 ASP.NET 页使用上一例中定义的自定义按钮 MyButton。[C#] <%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %> <script language="C#" runat=server>
a.ascx //控件
<script language="C#" runat="server">
void BeClicked(object sender,EventArgs e)
{
TextBox1.Text="Hello, world"
}
</script>
<asp:Button id="Button1" OnClick="BeClicked" runat="server" />
<asp:TextBox id="TextBox1" runat="server" />b.aspx //调用a.ascx控件的页面
<script language="c#" runat="server">
</script>
<form runat="server">
<a:a id="UserControl" runat="server" />
</form>现在是控件a.ascx 中处理button1的click事件,
但是我想在b.aspx中处理button1的click事件,该怎么弄
using System;
using System.Web.UI;namespace CustomControls
{
public class MyButton: Control, IPostBackEventHandler
{
// Defines the Click event.
public event EventHandler Click;
// Invokes delegates registered with the Click event.
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
// Method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
" Value = 'Click Me' />");
}
}
}
[Visual Basic]
Option Explicit
Option StrictImports System
Imports System.Web.UINamespace CustomControls
Public Class MyButton
Inherits Control
Implements IPostBackEventHandler
' Defines the Click event.
Public Event Click As EventHandler
' Invokes delegates registered with the Click event.
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
' Method of IPostBackEventHandler that raises change events.
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.Empty)
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write("<INPUT TYPE = submit name = " & Me.UniqueID & _
" Value = 'Click Me' />")
End Sub
End Class
End Namespace
在页上使用控件
下面 ASP.NET 页使用上一例中定义的自定义按钮 MyButton。[C#]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="C#" runat=server>
private void Button_Click(Object sender, EventArgs e) {
TextBox.BackColor = System.Drawing.Color.Green;
TextBox.Text = "You clicked the button";
}
</script>
<html>
<body>
<form runat=server>
Here is the custom button.<br>
<Custom:MyButton Id = "Button" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the button" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="VB" runat=server>
Private Sub Button_Click(sender As Object, e As EventArgs)
TextBox.BackColor = System.Drawing.Color.Green
TextBox.Text = "You clicked the button"
End Sub
</script>
<html>
<body>
<form runat=server>
Here is the custom button.<br>
<Custom:MyButton Id = "Button" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the button" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>
AddHandler aa.Click, AddressOf button1_Click