从用户控件调用父页面中的方法

时间:2009-03-08 05:53:00

标签: c# asp.net user-controls webusercontrol

我在aspx页面中注册了一个用户控件 在用户控件中按钮的单击事件中,如何调用父页面代码隐藏中的方法?

感谢。

10 个答案:

答案 0 :(得分:111)

以下是使用Freddy Rios(来自Web应用程序项目的C#)建议的事件的经典示例。这假定您要使用现有委托而不是自己创建委托,并且您没有通过事件参数传递任何特定于父页面的内容。

在用户控件的代码隐藏中(如果不使用代码隐藏或C#,则根据需要进行调整):

public partial class MyUserControl : System.Web.UI.UserControl
{
    public event EventHandler UserControlButtonClicked;

    private void OnUserControlButtonClick()
    {
        if (UserControlButtonClicked != null)
        {
            UserControlButtonClicked(this, EventArgs.Empty);
        }
    }

    protected void TheButton_Click(object sender, EventArgs e)
    {
        // .... do stuff then fire off the event
        OnUserControlButtonClick();
    }

    // .... other code for the user control beyond this point
}

在页面本身中,您使用以下内容订阅事件:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // hook up event handler for exposed user control event
        MyUserControl.UserControlButtonClicked += new  
                    EventHandler(MyUserControl_UserControlButtonClicked);
    }
    private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
    {
        // ... do something when event is fired
    }

}

答案 1 :(得分:14)

将页面转换为项目中的特定页面:

((MyPageName)this.Page).CustomMethod()

答案 2 :(得分:10)

我建议您不要直接调用页面方法,因为您将控件绑定到特定页面。

而是公开一个事件,并让页面订阅它。它适用于任意数量的页面,当控件在单个页面上多次(甚至可能在列表中)时更容易使用,并且更符合asp.control设计。

答案 3 :(得分:4)

Scott Allen有一篇关于从用户控件到父页面的事件冒泡的有用文章,详细阐述了Stephen M. Redd提供的答案:

答案 4 :(得分:4)

遵循良好和适当的方法,

使用事件和委托概念使委托与父页面中的方法具有相同的签名,然后在父页面加载事件中将父分配给它,然后通过用户控制中的事件调用此委托。

代码示例和链接如下。

//Parent Page aspx.cs part

 public partial class getproductdetails : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   ucPrompt.checkIfExist += new uc_prompt.customHandler(MyParentMethod);
  }

  bool MyParentMethod(object sender)
  {
   //Do Work
  }
}

//User control parts
public partial class uc_prompt : System.Web.UI.UserControl
{
 protected void Page_Load(object sender, EventArgs e)
 {
 }

 public delegate bool customHandler(object sender);
 public event customHandler checkIfExist;
 protected void btnYes_Click(object sender, EventArgs e)
 {
  checkIfExist(sender);
 }
}

详细了解其工作原理(参考): -

Calling a method of parent page from user control

答案 5 :(得分:2)

我想这样做:

  • 调用LoadEditCategory方法(父方法)。
  • 父方法(LoadEditCategory)需要int参数(CategoryID)。
  • 子用户控件是RightControlPanel,位于同一父页面文件夹中。

儿童用户控制

1-添加Action_LoadEditCategory

public Action<int> _LoadEditCategory = null;

<int>int参数(CategoryID)。

2-在按钮事件(Action按钮名称)中使用此btnSave,如下所示:

void btnSave_Click(object sender, EventArgs e)
{
    //123 is test integer for CategoryID
    _LoadEditCategory(123);        
}

父页面或父级用户控件

3-添加父方法

 private void LoadEditCategory(int CategoryID)
    {
     // CategoryID is 123 in my example
     //Do some things with CategoryID
    }

4-加载子用户控件(RightControlPanel

时添加此代码
//Load child user control
RightControlPanel si = this.LoadControl(this.ControlPath + "RightControlPanel.ascx") as RightControlPanel;
if (si != null) 
 {
   ...

   //For call parent method in child user control
   si._LoadEditCategory = c => LoadEditCategory(c);

   ...
 }

答案 6 :(得分:0)

 //c#
 //In parent page
 public void test(string S)
 {
    Label1.Text = S;
  }

 //In user control
 protected void Button1_Click(object sender, System.EventArgs e)
 {
 //Calling "test method" of parent page  from user control  
 ((object)this.Page).test("Hello!!");
 }

 'VB.Net 
'In parent page
 Sub test(ByVal S As String)
    Label1.Text = S
 End Sub

 'In user control
  Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
  'Calling "test method" of parent page  from user control  
  DirectCast(Me.Page, Object).test("Hello!!")
  End Sub 

答案 7 :(得分:0)

我喜欢Stephen M. Redd的回答并且不得不将其转换为VB。在这里分享。建议编辑欢迎。

在用户控件的代码隐藏中:

Public Class MyUserControl
    Inherits System.Web.UI.UserControl

    'expose the event publicly
    Public Event UserControlButtonClicked As EventHandler 

    'a method to raise the publicly exposed event
    Private Sub OnUserControlButtonClick()

        RaiseEvent UserControlButtonClicked(Me, EventArgs.Empty)

    End Sub

    Protected Sub lbtnApplyChanges_Click(sender As Object, e As EventArgs) Handles lbtnApplyChanges.Click

        'add code to run here, then extend this code by firing off this event
        'so it will trigger the public event handler from the parent page, 
        'and the parent page can hanlde it

        OnUserControlButtonClick()

    End Sub

End Class

在父页面中订阅该事件,因此在引发事件时,您的代码将在此处运行。

Public Class SalesRecord
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'hook up event handler for exposed user control event to handle this
        AddHandler MyUserControl.UserControlButtonClicked, AddressOf MyUserControl_UserControlButtonClicked

    End Sub

    ''' <summary>
    ''' code to run when user clicks 'lbtnApplyChanges' on the user control
    ''' </summary>

    Private Sub MyUserControl_UserControlButtonClicked()

        'this code will now fire when lbtnApplyChanges_Click executes

    End Sub

End Class

答案 8 :(得分:0)

我发布了一些适用于此的内容:Access control > page > master nested > master

或者,如果您要访问控件,也可以执行以下操作:

更新父页面 父级有一个名为“ UpdatePanel1”的更新面板

控制

UpdatePanel onParent1 = (UpdatePanel)Parent.FindControl("UpdatePanel1");
onParent1.Update();

答案 9 :(得分:-7)

您可以使用Session。假设当您单击用户控件中的按钮并且要调用父页面方法时,则在按钮的事件处理程序中单击设置值为

Session["CallParent"]="someValue";

按钮将回发页面。在父页面的Page_Load事件上添加它

protected void Page_Load(object sender,EventArgs e)
{
   if(Session["CallParent"]!=null)
   {
      // here call the Parent Page method 
      Method1();
      // now make the session value null
     Session["CallParent"]=null;
    }
}

效率更高