树视图和回发

时间:2010-03-18 08:23:15

标签: c# asp.net treeview postback

我有一个树视图,并且已经有回发页面的javascript代码。因为当我选择父亲时,自动选择孩子。

这是我的HTML代码

<script>
                                function postBackCheckBox()
                                {
                                    var o = window.event.srcElement;
                                    if (o.tagName == 'INPUT' && o.type == 'checkbox' && o.name != null && o.name.indexOf('CheckBox') > -1)
                                    {
                                        __doPostBack('LinkButton1', '');
                                    }
                                }
                            </script>

                            <asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:TreeView ID="HierarchicalTreeView" runat="server" OnSelectedNodeChanged="HierarchicalTreeView_SelectedNodeChanged"
                                        ShowLines="True" ExpandImageToolTip="Fechar &quot;{0}&quot;" CollapseImageToolTip="Fechar &quot;{0}&quot;"
                                        ExpandDepth="1" OnTreeNodeCheckChanged="HierarchicalTreeView_TreeNodeCheckChanged"
                                        EnableClientScripts="true">
                                        <NodeStyle CssClass="text" />
                                        <SelectedNodeStyle CssClass="text" Font-Bold="true" />
                                    </asp:TreeView>
                                </ContentTemplate>
                                </asp:UpdatePanel>
                            <asp:LinkButton ID="LinkButton1" runat="server" Visible="false"></asp:LinkButton>

和C#

HierarchicalTreeView.Attributes.Add("onclick", "postBackCheckBox()");
ScriptManager scripManager = (ScriptManager)Page.Master.FindControl("ScriptManagerMaster");
scripManager.RegisterAsyncPostBackControl(LinkButton1);

如何阻止回发所有页面,只有树视图???,所以不要'刷新'页面

3 个答案:

答案 0 :(得分:3)

<asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:TreeView ID="HierarchicalTreeView" runat="server" OnSelectedNodeChanged="HierarchicalTreeView_SelectedNodeChanged"
            ShowLines="True" ExpandImageToolTip="Fechar &quot;{0}&quot;" CollapseImageToolTip="Fechar &quot;{0}&quot;"
            ExpandDepth="10" 
            OnTreeNodeCheckChanged="HierarchicalTreeView_TreeNodeCheckChanged" 
            ontreenodeexpanded="HierarchicalTreeView_TreeNodeExpanded">
            <NodeStyle CssClass="text" />
            <SelectedNodeStyle CssClass="text" Font-Bold="true" />
        </asp:TreeView>                                                                     
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="buttonCheck" EventName="click" />
    </Triggers>
</asp:UpdatePanel>

<asp:button ID="buttonCheck" runat="server" CausesValidation="false" />

C#代码

 buttonCheck.Attributes.CssStyle["visibility"] = "hidden";
            HierarchicalTreeView.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", buttonCheck.ClientID));
            ScriptManager scripManager = (ScriptManager)Page.Master.FindControl("ScriptManagerMaster");
            scripManager.RegisterAsyncPostBackControl(buttonCheck);

答案 1 :(得分:1)

您需要使用Ajax,例如将您的树视图放在UpdatePanel

尝试像这样调用__dopostback函数:

 __doPostBack('<%= LinkButton1.ClientID %>', '');

答案 2 :(得分:1)

允许在树视图中使用复选框但是没有为它设置事件处理程序是愚蠢的...如果您不将SelectNodeChanged用于任何其他目的,这是一个解决方法,试试这个,而不是直接单击复选框,您单击节点,但你的用户需要被告知:)

Protected Sub tvCust_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvCust.SelectedNodeChanged
    Dim enode As TreeNode = tvCust.SelectedNode
    If enode.Checked = False Then
        enode.Checked = True
    Else
        enode.Checked = False
    End If
    tvCust_TreeNodeCheckChanged(enode)
End Sub

Protected Sub tvCust_TreeNodeCheckChanged(ByVal e As TreeNode)
    Try
        Select Case e.Depth
            Case 0  'parent
                Dim child As TreeNode
                For Each child In e.ChildNodes
                    child.Checked = e.Checked
                Next
            Case Else
                Dim cnt As Integer = 0
                Dim child As TreeNode
                For Each child In e.Parent.ChildNodes
                    If child.Checked = True Then
                        cnt += 1
                    End If
                Next
                If cnt = e.Parent.ChildNodes.Count Then
                    e.Parent.Checked = True
                Else
                    e.Parent.Checked = False
                End If
        End Select

    Catch ex As Exception

    End Try
End Sub