首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户控件事件处理程序

用户控件事件处理程序
EN

Stack Overflow用户
提问于 2012-05-16 15:06:45
回答 1查看 298关注 0票数 0

我在C#中有两个用户控件(webheader,webfooter)

代码语言:javascript
复制
   <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="webheader.ascx.cs"     Inherits="WebUserControls.webheader" %>
   <p>
    <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />

    &nbsp;&nbsp;&nbsp;&nbsp;

    <asp:Button ID="btnEdit" runat="server" Text="Edit" />

    &nbsp;&nbsp;&nbsp;&nbsp;

    <asp:Button ID="btnDelete" runat="server" Text="Delete" />

  </p>


  <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="webFooters.ascx.cs"      Inherits="WebUserControls.webFooters" %>
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <br />
  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  <br />
  <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
   <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  <br />
  <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
  <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  <br />
  <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
  <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
   <p>
    &nbsp;</p>

我在usercontrol网页页眉中有3个按钮,我想在网页页脚中调用3个不同的方法,每个按钮调用不同的方法,既然我把2个usercontrol放在一个页面上,我该如何处理呢?

EN

回答 1

Stack Overflow用户

发布于 2012-05-16 15:40:32

在Events和Delegates的帮助下,你可以很容易地做到这一点。

公开来自WebHeader的事件并在内容页面中订阅它。使您希望在WebHeader单击时调用的WebFooter中的方法成为公共方法。单击该按钮后,调用WebFooter的appropiate方法

代码语言:javascript
复制
//pseudo code

//WebHeader
public delegate void Button1Clicked(object[] args);
//raise this event when the button is clicked
public event Button1Clicked buttonClicked;

//WebFooter
public void Method1(object[] params); //for button 1 click



//In content page which holds both header and footer control
WebHeader.Button1Clicked+= new WebHeader.Button1Clicked (HandleClick);

public void HandleClick(object[] params)
{
    WebFooter.Method2(params);
}

有关更清晰的http://www.dotnetfunda.com/articles/article201.aspx,请参阅以下示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10613607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档