如何在Treeview Checkbox的__doPostBack上使用UpdatePanel检查/取消选中事件来避免页面刷新

时间:2017-04-04 12:14:25

标签: asp.net treeview postback page-refresh

当我选中Treeview的复选框时,我需要执行一些任务。所以,我使用__doPostBack创建了一个asyncrhonous回发,它完美地工作。但是,当我在TreeView复选框上执行检查/取消选中操作时,它正在刷新页面。我将TreeView保留在Updatepanel中,但它也无法正常工作。这是我的代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function TreeViewCheckBoxClicked(Check_Event) {
            var objElement;
            try {
                // Get the element which fired the event.
                objElement = window.event.srcElement;
            }
            catch (Error) {
                //srcElement is failing, objElement is null.
            }
            if (objElement != null) {
                // If the element is a checkbox do postback.
                if (objElement.tagName == "INPUT" && objElement.type == "checkbox") {
                    __doPostBack("", "");
                }
            }
            else {
                //    If the srcElement is failing due to browser incompatibility determine
                // whether the element is and HTML input element and do postback.
                if (Check_Event != null) {
                    if (Check_Event.target.toString() == "[object HTMLInputElement]") {
                        __doPostBack("", "");
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" onclick="TreeViewCheckBoxClicked(event)">
                    <Nodes>
                        <asp:TreeNode Text="TestTreeView" Value="TestTreeView">
                            <asp:TreeNode ShowCheckBox="True" Text="TestNode1" Value="TestNode1">
                                <asp:TreeNode ShowCheckBox="True" Text="TestNode1Visible" Value="TestNode1Visible"></asp:TreeNode>
                                <asp:TreeNode ShowCheckBox="True" Text="TestNode1Label" Value="TestNode1Label"></asp:TreeNode>
                            </asp:TreeNode>
                            <asp:TreeNode ShowCheckBox="True" Text="TestNode2" Value="TestNode2">
                                <asp:TreeNode ShowCheckBox="True" Text="TestNode2Visible" Value="TestNode2Visible"></asp:TreeNode>
                                <asp:TreeNode ShowCheckBox="True" Text="TestNode2Label" Value="TestNode2Label"></asp:TreeNode>
                            </asp:TreeNode>
                        </asp:TreeNode>
                    </Nodes>
                </asp:TreeView>
            </ContentTemplate>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>

请帮我解决这个问题。

提前致谢。

0 个答案:

没有答案