用户控件使用事件与调用页面交互
2007-07-29 14:33
507 查看
1.定义事件参数类
using System;
namespace ASPNetCookbook.CSExamples
{
/// <summary>
/// This class provides the definition of the custom event arguments used
/// as the event arguments for the message sent from this control. This
/// class simply inherits from System.EventArgs and adds a message property.
/// </summary>
public class MessageEventArgs
{
private String mMessage;
/// <summary>
/// This property provides the ability to get/set the message in the
/// event args
/// </summary>
public String message
{ get
{
return (mMessage);
}
set
{
mMessage = value;
}
} // message
} // MessageEventArgs
}
2.定义控件代码
Test.ascx
Test.aspx.cs
using System;
namespace ASPNetCookbook.CSExamples
{
/// <summary>
/// This class provides the definition of the custom event arguments used
/// as the event arguments for the message sent from this control. This
/// class simply inherits from System.EventArgs and adds a message property.
/// </summary>
public class MessageEventArgs
{
private String mMessage;
/// <summary>
/// This property provides the ability to get/set the message in the
/// event args
/// </summary>
public String message
{ get
{
return (mMessage);
}
set
{
mMessage = value;
}
} // message
} // MessageEventArgs
}
2.定义控件代码
Test.ascx
<asp:Button ID="btnSendMessage" runat="server"
Text="Send Message"
OnClick="btnSendMessage_Click" />
Text.ascx.cs
using System;
namespace ASPNetCookbook.CSExamples
{
/// <summary>
/// This class provides the code-behind for
/// CH05UserControlCommSourceCS.ascx
/// </summary>
public partial class CH05UserControlCommSourceCS : System.Web.UI.UserControl
{
// define the delegate handler signature and the event that will be raised
// to send the message
public delegate void customMessageHandler(Object sender,
MessageEventArgs e);
public event customMessageHandler OnSend;
///***********************************************************************
/// <summary>
/// This routine provides the event handler for the send message button
/// click event. It creates a new MessageEventArgs object then raises
/// an OnSend event
/// </summary>
///
/// <param name="sender">Set to the sender of the event</param>
/// <param name="e">Set to the event arguments</param>
protected void btnSendMessage_Click(object sender,
EventArgs e)
{
MessageEventArgs messageArgs = new MessageEventArgs();
messageArgs.message = "This message came from the source user control";
if (OnSend != null)
{
OnSend(this, messageArgs);
}
} // btnSendMessage_Click
} // CH05UserControlCommSourceCS
}
调用页面调用
Test.aspx
<asp:lable id="labMessage" runat="server"/>
<table width="60%" align="center" border="0">
<tr>
<td class="PageHeading">
Source User Control:
</td>
</tr>
<tr>
<td bgcolor="#ffffcc" align="center" height="75">
<ASPCookbook:SourceControl id="ucSource" runat="server" />
</td>
</tr>
</table>
Test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//在调用页面中挂接控件事件
ucSource.OnSend +=
new CH05UserControlCommSourceCS.customMessageHandler(this.updateLabel);
}
private void updateLabel(Object sender,
MessageEventArgs e)
{
// update the label with the message in the event arguments
labMessage.Text = e.message;
}
相关文章推荐
- 用户控件使用事件与调用页面交互
- 使用C#反射实现用户控件调用父页面方法
- 关于enum ,调用webservice,用户控件与主页面之间的交互,datsource属性,net面试题,反射类生成sql语句,URl重写一个小实例
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
- Asp.net开发心得点滴[动态加载的用户控件使用事件委托,交给页面处理的事件无效问题]
- 使用委托实现用户控件与父页面的交互
- 使用C#反射实现用户控件调用父页面方法
- 一个aspx页面有2个按钮,都是服务器控件,服务器上的DLL如何知道用户点击了哪个按钮,从而按钮的事件函数被调用
- 在用户控件中如何调用主页面的事件之事件委托
- 在用户控件中如何调用主页面的事件之页面反射
- 用户控件使用委托把事件交给页面处理
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
- 使用委托实现用户控件与父页面的交互
- 页面上调用用户控件的事件
- 使用委托实现用户控件与父页面的交互
- ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用
- 用户控件调用父页面方法
- 用户控件事件使用delegate
- Datalist控件事件使用及调用模板内控件
- ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用