如何从ContentPage上的按钮更新母版上的GridView

本文关键字:GridView 更新 按钮 ContentPage | 更新日期: 2023-09-27 18:08:21

我有一个MasterPage与GridView上的UpdatePanel。在我的一个内容页面上,我有一个按钮,它将项目添加到一个会话,我想出现在主页面上的Gridview中。我在Gridview中有项目,但有刷新或回发之类的问题。有人能回答这个问题吗?

如何从ContentPage上的按钮更新母版上的GridView

如果你在updatepanel中有刷新问题,那么这意味着post back按钮要么不在更新面板内,要么面板没有手动更新。

对于这种情况,我假设你不能把按钮在面板内,因为它是内容页的一部分,所以我建议你设置面板的UpdateMode条件,并有一些刷新方法在你的主页。为了在内容页中看到这个方法,用这个方法制作一些接口,让母版页使用这个接口。

,然后在内容页中获取母版页引用并使用刷新方法。

public interface IMaster
{
    void RefreshPanel();
}

masterpage

(注意它使用了我们之前创建的IMaster接口)

public partial class MasterPage : System.Web.UI.MasterPage,  IMaster
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Load items from session
    }
    public void RefreshPanel()
    {
        UpdatePanel1.Update();
    }
}

内容页

public partial class ContentPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       //Add items to session 
       //....
        //Now refresh the updatepanel on the masterpage
        IMaster masterPage = Master as IMaster;
        masterPage.RefreshPanel();
    }
}

您需要研究使用事件和委托。基本上,您将在用户控件上创建一个事件,您的主页将对该事件作出反应。还有很多其他网站的例子,所以只要谷歌ASP。. NET Events and delegate .