ASP.NET - Update panel in Master page, refresh button in Content Page

13k views Asked by At

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?

1

There are 1 answers

1
mellamokb On BEST ANSWER

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.