2012-05-16 113 views
0

我有在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> 

我已经在用户控件3个按钮webheader我想呼叫在webfooters 3种不同的方法被称为每个按钮不同方法我该如何处理,因为我把2个用户控件放在一个页面上

回答

0

在事件和委托的帮助下,您可以非常轻松地完成此任务。

从WebHeader公开事件并在内容页面中订阅它。使WebFooter中的方法在WebHeader Click上调用。一旦按钮被点击通话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

+0

我会尝试,我会让你知道结果 –

+0

感谢阿南德它是为我工作的好是我想要的 –

+0

@RabihGebara:如果它解决了您的查询,请将答案标记为正确。谢谢 – Anand