ASP.NET - 母版页中的更新面板,内容页中的刷新按钮

发布于 2024-10-19 11:48:47 字数 1941 浏览 8 评论 0原文

我在主页中有更新面板:

<asp:ScriptManager id="CartScript" runat="server"></asp:ScriptManager>       
<asp:UpdatePanel id="CartBox" runat="server" updateMode="Conditional">
  <ContentTemplate>
    Košík [ <asp:HyperLink NavigateUrl="~/Account/Login.aspx" ID="ShoppingCart" runat="server" text="" /> ] <asp:LinkButton ID="DeleteCart" runat="server" Text="Vymazat košík" OnClick="ThrowCart_Click" />
  </ContentTemplate>
</asp:UpdatePanel>

在内容页面有购买按钮:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">    
  <asp:Button ID="BuyButton" Runat="server" Text="Přidat do košíku" onclick="Buy_Click" />
</asp:Content>

所以我需要为此按钮添加到更新面板 AsyncPostBackTrigger。

首先,我尝试从内容页面添加它:

protected void Page_Load(object sender, EventArgs e)  
{  
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();         
    trigger.ControlID = "BuyButton";  
    UpdatePanel panel = (UpdatePanel)Master.FindControl("CartBox");   
    if (panel != null)  
    {  
        panel.Triggers.Add(trigger);  
    }  
    ScriptManager script = (ScriptManager)Master.FindControl("CartScript");  
    script.RegisterAsyncPostBackControl(BuyButton);  
}

但它出现错误:在 UpdatePanel 'CartBox' 中找不到触发器的 ID 为 'BuyButton' 的控件。

所以我尝试从母版页添加它:

protected void Page_Load(object sender, EventArgs e)
{                
    if ((Button)MainContent.FindControl("BuyButton")!=null)
    {
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = ((Button)MainContent.FindControl("BuyButton")).ID;
        CartBox.Triggers.Add(trigger);
        CartScript.RegisterAsyncPostBackControl((Button)MainContent.FindControl("BuyButton"));
    }
}

但我遇到了同样的错误。 :-(

那么你能告诉我如何添加到我的更新面板中,内容页面中的按钮可以刷新它吗?

I have Update panel in Master page:

<asp:ScriptManager id="CartScript" runat="server"></asp:ScriptManager>       
<asp:UpdatePanel id="CartBox" runat="server" updateMode="Conditional">
  <ContentTemplate>
    Košík [ <asp:HyperLink NavigateUrl="~/Account/Login.aspx" ID="ShoppingCart" runat="server" text="" /> ] <asp:LinkButton ID="DeleteCart" runat="server" Text="Vymazat košík" OnClick="ThrowCart_Click" />
  </ContentTemplate>
</asp:UpdatePanel>

and Buy Button in Content page:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">    
  <asp:Button ID="BuyButton" Runat="server" Text="Přidat do košíku" onclick="Buy_Click" />
</asp:Content>

So I need add to Update panel AsyncPostBackTrigger for this button.

First i tryed add it from content page:

protected void Page_Load(object sender, EventArgs e)  
{  
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();         
    trigger.ControlID = "BuyButton";  
    UpdatePanel panel = (UpdatePanel)Master.FindControl("CartBox");   
    if (panel != null)  
    {  
        panel.Triggers.Add(trigger);  
    }  
    ScriptManager script = (ScriptManager)Master.FindControl("CartScript");  
    script.RegisterAsyncPostBackControl(BuyButton);  
}

But it did error: A control with ID 'BuyButton' could not be found for the trigger in UpdatePanel 'CartBox'.

So i tried it add from Master page:

protected void Page_Load(object sender, EventArgs e)
{                
    if ((Button)MainContent.FindControl("BuyButton")!=null)
    {
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = ((Button)MainContent.FindControl("BuyButton")).ID;
        CartBox.Triggers.Add(trigger);
        CartScript.RegisterAsyncPostBackControl((Button)MainContent.FindControl("BuyButton"));
    }
}

But i got same error. :-(

So can u tell me how I can add to my Update Panel that Button from Content Page can refresh it?

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

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

发布评论

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

评论(1

耳根太软 2024-10-26 11:48:47

编辑

BuyButton 按钮放置在内容页面上其自己的 UpdatePanel 内。将您的 AsyncPostBackTrigger 添加到新的 UpdatePanel,然后它将能够找到该按钮,并使用母版页中的 ScriptManager

Edit

Place the BuyButton button inside of its own UpdatePanel as well on the content page. Add your AsyncPostBackTrigger to that new UpdatePanel, and then it will be able to find the button, and will use the ScriptManager from the master page.

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