Ajax callback UpdatePanel.Update() still reloading whole page

16,656

Solution 1

Where is the button on Gifts.ASPX? If you put the button inside the UpdatePanel or use triggers you don't need to call UpdatePanel3.Update(); from the code behind.

Solution 2

Where is the button in the above example? Inside or outside the UpdatePanel. If it is outside you will need to add it to the triggers collection of the UpdatePanel.

Also you only need to call UpdatePanel.Update() if you are changing the content of an UpdatePanel other than the one that caused the (Partial) postback.

As a side note (and personal crusade), it is recommended that a using statement is put around your DB connection.

With the markup below, the following will happen:

  • btnInnerPart is inside the update panel, so it will automatically cause a partial postback
  • btnInnerFull will cause a full postback as it has a PostBackTrigger in the trigger collection
  • btnOuterPart will cause a partial postback as it has an AsyncPostBackTrigger in the trigger collection
  • btnOuterFull will cause a full postback as it is outside the UpdatePanel

Markup:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <!-- Content -->
        <asp:Button runat="server" ID="btnInnerPart" Text="Inner Part" />
        <asp:Button runat="server" ID="btnInnerFull" Text="Inner Full" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnOuterPart" />
        <asp:PostBackTrigger ControlID="btnInnerFull" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnOuterFull" Text="Outer Full" />
<asp:Button runat="server" ID="btnOuterPart" Text="Outer Part" />

Solution 3

Also, You need to have a ScriptManager object on your page. Do you have one?

Share:
16,656
leora
Author by

leora

Updated on August 23, 2022

Comments

  • leora
    leora over 1 year

    I have code in an Update Panel and even though on a button click i am inserting data into a db and simply calling Updatepanel.Update() the whole page is reloaded:

    Gifts.ASPX

    <table style="width:100%;">
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Gift"></asp:Label>
                    </td>
                    <td>
    <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
                        <asp:TextBox ID="txtNewGift" runat="server"></asp:TextBox>
        </ContentTemplate>
    </asp:UpdatePanel>
                    </td>
                </tr>
                <tr>
    

    Gifts.aspx.CS

    protected void cmdAddGift_Click(object sender, EventArgs e)
    {
        OleDbConnection objConn = new OleDbConnection(DataSource);
    
        Random r = new Random();
        int giftID = r.Next(1200, 14000);
    
        OleDbCommand objCommand = new OleDbCommand("Insert into Gifts (GiftID, Description) values (" + giftID + ",'" + txtNewGift.Text + "')", objConn);
        ExecuteCommand(objCommand);
    
        PopulateGifts(objConn);
    
        txtNewGift.Text = "";
        UpdatePanel3.Update();
    }
    

    Any ideas why this whole page would reload instead of just the textbox getting update?