如何知道asp中树视图控件的回发?

时间:2013-08-19 12:54:42

标签: asp.net treeview

有没有办法知道回发是否是通过展开或折叠树视图控件生成的?

2 个答案:

答案 0 :(得分:0)

你可以试试这个

if (IsPostBack)
{
    string ControlID = string.Empty;
    if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
    {
        ControlID = Request.Form["__EVENTTARGET"];
    }
}

来源:http://www.codeproject.com/Articles/134614/Way-To-Know-Which-Control-Has-Raised-PostBack

另一个链接:http://www.codeproject.com/Tips/82086/How-to-know-which-control-has-raised-a-postback

答案 1 :(得分:0)

您的ASP.NET TreeView:

<asp:TreeView ID="tvMain" runat="server" ExpandDepth="10"     OnTreeNodeExpanded="tvMain_TreeNodeExpanded" OnTreeNodeCollapsed="tvMain_TreeNodeCollapsed">
    <RootNodeStyle Font-Bold="true" />
    <Nodes>
        <asp:TreeNode Text="Root" Value="Root">
            <asp:TreeNode Text="Link2" Value="Link1"></asp:TreeNode>
            <asp:TreeNode Text="Link2" Value="Link2"></asp:TreeNode>
        </asp:TreeNode>

    </Nodes>
</asp:TreeView>

您的代码背后:

Protected Sub tvMain_TreeNodeCollapsed(sender As Object, e As System.Web.UI.WebControls.TreeNodeEventArgs)
    If Page.IsPostBack Then
        If e.Node.Value.Equals("Root") Then
            'Do your stuff'
        End If
    End If
End Sub


Protected Sub tvMain_TreeNodeExpanded(sender As Object, e As System.Web.UI.WebControls.TreeNodeEventArgs)
    If Page.IsPostBack Then
        If e.Node.Value.Equals("Root") Then
            'Do your stuff'
        End If
    End If
End Sub

诀窍是处理OnTreeNodeCollapsed事件和OnTreeNodeExpanded事件。接下来,为rootnode选择一个Value - 这是第一个和单个子节点。您可以将节点添加到根节点,并确保它们具有与根节点不同的值。