locked
Update updatepanel if ANY control is clicked inside updatepanel RRS feed

  • Question

  • User-941706905 posted

    Hello,

    I wonder if it is possible to run a C# function if ANY control that exists inside the updatepanel is changed. This code works if any of the 2 dropdownlists is changed or if the button is clicked. So I have put all 3 controlIDs in the "<Triggers>".

    The problem now is that I will have about 200 dropdownlists and 200 buttons and like to avoid putting all of them in the "<Triggers>".

    I wonder if that is possible to do and if so how to do that for the below code?

    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true" >
    <ContentTemplate>
    
    
      <asp:Button ID="Button1" runat="server" Text="button" Width="271px" OnClick="dosomething_Click" />
    
    
      <asp:DropDownList ID="DropDownList1" runat="server" Width="121px" AutoPostBack="true" onselectedindexchanged="dosomething_Click">
                                  <asp:ListItem>1</asp:ListItem>
                                  <asp:ListItem>2</asp:ListItem>
                              </asp:DropDownList>
    
      <asp:DropDownList ID="DropDownList2" runat="server" Width="121px" AutoPostBack="true" onselectedindexchanged="dosomething_Click">
                                  <asp:ListItem>1</asp:ListItem>
                                  <asp:ListItem>2</asp:ListItem>
                              </asp:DropDownList>
    
    </ContentTemplate>   
    <Triggers>    
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click"/> 
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
        <asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" /> 
    </Triggers>  
    </asp:UpdatePanel>  
    
    
    
    
    
    //C# code
            protected void dosomething_Click()
            {
                //dosomething
            }

    Thursday, September 1, 2016 4:25 PM

Answers

  • User-1142886626 posted

    Hi Silvers,

    The problem now is that I will have about 200 dropdownlists and 200 buttons and like to avoid putting all of them in the "<Triggers>".

    Maybe you could use JavaScript function to achieve this. You could use jquery selector find all button and dropdownlist on your page, then bind the event.

    It might also be noted that some JavaScript function should be put in the UpdatePanel.

    Code below is for your reference:

    <script type="text/javascript">
            function BindEvent() {
                $(function () {
                    $(":submit").bind("click", function () {  
                            alert("asd");
                    })
                    $("select").bind("change", function () {
                        var tx=$(this).find("option:selected").text();
                        if (tx== 1) {
                            alert(1);
                        } else if (tx== 2) {
                            alert(2);
                        }
                    })
            })
            }
        </script>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <script type="text/javascript">
                        Sys.Application.add_load(BindEvent);
                    </script>
                    <asp:Button ID="Button1" runat="server" Text="Button" /><br />
                    <asp:Button ID="Button2" runat="server" Text="Button" /><br />
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>select</asp:ListItem>
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem>select</asp:ListItem>
                         <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                    </asp:DropDownList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    

    Best Regards,

    Ailleen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 2, 2016 7:35 AM