How to conditional update UpdatePanel in Asp.Net

Leave a Comment

If more than one UpdatePanel is placed on the page and there is a need of

  1. Updating only a specific UpdatePanel, we can set UpdateMode as “Conditional” to all the UpdatePanels so that only corresponding UpdatePanel will update on an action.
  2. Updating all UpdatePanel on any action, we can set UpdateMode as “Always” to all the UpdatePanels that will update all UpdatePanels if any action is performed for an UpdatePanel.
Demo Example
In order to create conditional updatepanel demo, first create UpdatePanleConditional.aspx page and add below code.
<asp:ScriptManager runat="server" ID="SM1" />
        Click on the Submit button and notice that only 1st time updates. Now change the UpdateMode (Always) of 2nd UpdatePanel and run the page, click on Submit button. Notice that both time is updating.
       
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <h4>Inside UpdatePanel - 1</h4>
                <asp:Label ID="lblUpdatePanel" runat="server" EnableViewState="false" /> <br />
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="RefreshTheTime" />
            </ContentTemplate>
        </asp:UpdatePanel>

        <!-- Change UpdateMode and see the difference -->
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
            <h4>Inside UpdatePanel - 2</h4>
                <asp:Label ID="lblUpdatePanel1" runat="server" EnableViewState="false" /> <br />
            </ContentTemplate>
        </asp:UpdatePanel>

        <hr />
        <h4>Outside UpdatePanel</h4>
        <asp:Label ID="lblOutSideUpdatePanel" runat="server" EnableViewState="false" />

Now, add below code to UpdatePanelConditional.aspx.cs page.
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblOutSideUpdatePanel.Text = DateTime.Now.ToString();
            lblUpdatePanel.Text = DateTime.Now.ToString();
            lblUpdatePanel1.Text = DateTime.Now.ToString();
        }
    }
    protected void RefreshTheTime(object sender, EventArgs e)
    {
        lblUpdatePanel.Text = DateTime.Now.ToString();
        lblUpdatePanel1.Text = DateTime.Now.ToString();
    }


0 comments:

Post a Comment