Search code examples
c#asp.nettelerikupdatepanel

What is the correct way to put multiple controls inside update panel?


I have one registration form which contains 3 to 4 dropdown controls and 2 datepickers and now when dropdown controls value are selected(selectedindex change are fired) then i dont want my page to postback.

I have use update panel to stop this behaviour of post like below:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

      <%--Update Panel for date picker%>
      <asp:UpdatePanel ID="UpdatePanelDatepicker" runat="server">
                    <ContentTemplate>
                      <telerik:RadDatePicker ID="rdpDate1" runat="server">
                      </telerik:RadDatePicker>
                    </ContentTemplate>
      </asp:UpdatePanel>

       <%--Update Panel for Dropdown--%>
       <asp:UpdatePanel ID="updatepaneldata" runat="server"> 
                      <ContentTemplate>
                     <telerik:RadComboBox ID="ddlCountry" runat="server">
                      </telerik:RadComboBox>
                    </ContentTemplate>
      </asp:UpdatePanel>


  </ContentTemplate>
    </asp:UpdatePanel>

So i just wanted to ask that is this correct way to put multiple controls under update panels??


Solution

  • Subscribe to ajax event of initializeRequest on client-side. In this event we can cancel an ajax postback if we need to.

    The initializeRequest method is raised before processing of the asynchronous request starts. You can use this event to cancel a postback.

    In this event, we will check if an async postback is being initiated due to ddlCountry, and if yes then we cancel the ajax post back so no post back occurs.

    To solve your problem just do the following : Add following JavaScript to your aspx page. In code below, the pageLoad method is automatically called by ASP.Net Framework on client-side when browser loads the page and after all scripts have been loaded as well as all client-side objects created.

    JavaScript to cancel combobox post back

    <script type="text/javascript">
    function pageLoad()
    {
         Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CancelComboBoxPostback);
    }
    
    function CancelComboBoxPostback(sender, args)
    {
      var prm = Sys.WebForms.PageRequestManager.getInstance();
      if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id == 'ddlCountry') {
             args.set_cancel(true);
        }
     }
    </script>
    

    The following is only a recommendation and not a part of the solution to your specific problem: Also, I would recommend to stay away from nested update panels as this can cause unexpected results if developer is not aware of how nested update panels work. In your situation, a single update panel should suffice as in markup below instead of nested update panels that you have used in your original markup.

    Markup without nested update panels

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                  <telerik:RadDatePicker ID="rdpDate1" runat="server">
                  </telerik:RadDatePicker>
    
                  <telerik:RadComboBox ID="ddlCountry" runat="server">
                  </telerik:RadComboBox>
        </ContentTemplate>
    </asp:UpdatePanel>