调用 ASP.NET 用户控件事件时调用页面事件

发布于 2024-12-10 12:13:25 字数 950 浏览 0 评论 0原文

假设页面中有一个名为 Paging.ascx 的用户控件,该控件嵌入在 PageWithResults.aspx 中。该控件具有必要的属性来跟踪有关您所在页面的各种详细信息(即:CurrentPage、TotalRecordsInResults、RecordsPerPage 等)。它还包含单击超链接(“下一页”或“上一页”)时触发的事件。下面的例子。我需要告诉 PageWithResults.aspx 这些 LinkBut​​ton Web 控件之一已被单击。我想我需要在页面中分配一个委托,以便当调用此用户控件事件(单击超链接)时,它还会调用页面类中的一些其他方法/事件。这样我就可以快速检查 CurrentPage 的新值是什么(基于下面的事件中调用的内容)并获取新页面的新结果集(基于 CurrentPage 属性)。但我不确定最好的方法。我认为这需要一个代表,但我不知道如何连接它。如果您需要更多详细信息,请询问。

    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.CurrentPage + 1;
        if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
    }

我想我必须把我的代表安排在这里某个地方。 ??

    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.CurrentPage + 1;
        if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
                    //delegate to call object.method or something
    }

Suppose there is a user control in a page called Paging.ascx that is embedded in PageWithResults.aspx. This control has the necessary properties to keep track of various details about what page you're on (ie: CurrentPage, TotalRecordsInResults, RecordsPerPage, etc..). It also contains events that fire off when you click on a hyperlink ("next page" or "previous page"). Example below. I need to tell PageWithResults.aspx that one of these LinkButton web controls was clicked. I think I need to assign a delegate in the page, so that when this user control event is called (hyperlink is clicked), it also calls some other method/event in the page class. That way I can quickly check what the new value of CurrentPage is (based on what was called in the event below) and get a new result set for the new page (based on the CurrentPage property). But I'm not sure of the best approach. I'm thinking this will require a delegate, but I'm not sure how to wire it up. If you need more details, please ask.

    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.CurrentPage + 1;
        if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
    }

I'm thinking I have to put my delegate here somewhere. ??

    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.CurrentPage = this.CurrentPage + 1;
        if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
                    //delegate to call object.method or something
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-12-17 12:13:25

使用事件会很好地工作。

您可以像这样在 UserControl 中创建事件:

public event EventHandler ButtonClicked;

然后在需要时调用事件:

protected void Button1_Click(object sender, EventArgs e)
{
    if (ButtonClicked != null)
        ButtonClicked(this, new EventArgs());
}

在您的页面中,您需要分配一个事件处理程序:

protected void Page_Load(object sender, EventArgs e)
{
    UserControl1.ButtonClicked += new EventHandler(UserControl1_ButtonClicked);
}

void UserControl1_ButtonClicked(object sender, EventArgs e)
{

}

除了使用上述方法外,您还可以在 UserControl 中强制转换 Page 引用并调用直接公开方法:

MyPage page = (MyPage)Page;
page.AMethod();

希望这有帮助。

Using an event would work fine.

You would create the event within your UserControl like so:

public event EventHandler ButtonClicked;

Then invoke the event when required:

protected void Button1_Click(object sender, EventArgs e)
{
    if (ButtonClicked != null)
        ButtonClicked(this, new EventArgs());
}

In your page you would need to assign an event handler:

protected void Page_Load(object sender, EventArgs e)
{
    UserControl1.ButtonClicked += new EventHandler(UserControl1_ButtonClicked);
}

void UserControl1_ButtonClicked(object sender, EventArgs e)
{

}

As well as using the above approach you can also cast the Page reference in the UserControl and call a public method directly:

MyPage page = (MyPage)Page;
page.AMethod();

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文