Usercontrol事件处理程序

时间:2012-05-16 07:06:45

标签: c# asp.net

我在C#中有2个usercontrol(webheader,webfooter)

   <%@ 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 webheader中有3个按钮我想在webfooters中调用3个不同的方法每个按钮调用不同的方法我怎么能处理,因为我把2个usercontrol放在页面上

1 个答案:

答案 0 :(得分:0)

您可以在事件和代表的帮助下轻松完成。

从WebHeader公开事件并在内容页面中订阅它。在WebFeader上调用要在WebHeader上调用的WebFooter方法单击。单击按钮后,请调用WebFooter的适当方法

//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