调用 ASP.NET 用户控件事件时调用页面事件
假设页面中有一个名为 Paging.ascx 的用户控件,该控件嵌入在 PageWithResults.aspx 中。该控件具有必要的属性来跟踪有关您所在页面的各种详细信息(即:CurrentPage、TotalRecordsInResults、RecordsPerPage 等)。它还包含单击超链接(“下一页”或“上一页”)时触发的事件。下面的例子。我需要告诉 PageWithResults.aspx 这些 LinkButton 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用事件会很好地工作。
您可以像这样在 UserControl 中创建事件:
然后在需要时调用事件:
在您的页面中,您需要分配一个事件处理程序:
除了使用上述方法外,您还可以在 UserControl 中强制转换 Page 引用并调用直接公开方法:
希望这有帮助。
Using an event would work fine.
You would create the event within your UserControl like so:
Then invoke the event when required:
In your page you would need to assign an event handler:
As well as using the above approach you can also cast the Page reference in the UserControl and call a public method directly:
Hope this helps.