简体   繁体   English

从父页面调用Web用户控件的功能

[英]Call a function of a Web User Control from the parent page

I'm creating a comments web user control. 我正在创建一个注释Web用户控件。 I want to use this comments control on distinct pages like: articles, attractions and categories. 我想在不同的页面上使用此注释控件,例如:文章,景点和类别。

So, on the articles page I declare the Web User Control 因此,在文章页面上,我声明了Web用户控件

 <uc1:Comments ID="Comments1" runat="server"  />

On the control, there is a function call loadComments 在控件上,有一个函数调用loadComments

public void LoadComents(int ID,string type)
{
        //Load the comments into an asp.net repeater that resides on the control 
}

which I want to call once I have validated that the article exists. 验证文章是否存在后,我想调用该方法。 So on the article page I want do something like 所以在文章页面上我想做类似的事情

Comments1.LoadComents(101,"article");

Is this possible? 这可能吗?

You can use code like this from inside of your Page class: 您可以在Page类的内部使用以下代码:

Comments commentsControl = this.FindControl("Comments1");
commentsControl.LoadComents(1, "someType");

Or if the control exists on the page in Designer, you should be able to do something as simple as: 或者,如果控件存在于Designer的页面上,则应该能够执行以下操作:

this.Comments1.LoadComents(1, "someType");

On the UserControl Code-Behind I have the following code: 在后面的UserControl代码上,我有以下代码:

    public void SetPageTitle(string strTitle)
    {
        lPageTitle.Text = strTitle;
    }

On the Page Code-Behind that contains/included the UserControl 在包含/包含UserControl的页面Code-Behind上

   {
       PageTitle.SetPageTitle(cat.categoryName);
   }

This is working ... 这正在工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM