在 Treeview 节点上绑定 Gridview 单击

Bind Gridview on Treeview Node Click

我想在 treenode 上绑定 gridview click.code 工作正常,没有错误,但在 UI 中没有任何变化,但是当我在单击按钮时使用相同的代码,Gridview 正确绑定数据。

我的 apsx 代码是

  <asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                    <div style="overflow: scroll; height: 450px;">
                        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
                            <ContentTemplate>
                                <asp:TreeView ID="Folder_Treeview" runat="server" ShowLines="true" LeafNodeStyle-CssClass="childnode"
                                    Style="" ForeColor="Blue" SelectedNodeStyle-ForeColor="Green" OnSelectedNodeChanged="Folder_Treeview_SelectedItemChanged">
                                </asp:TreeView>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </div>
 <asp:GridView ID="GridView1" CssClass="grid" GridLines="None" ShowFooter="true" AllowPaging="true"
                        PageSize="5" AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging"
                        runat="server">
                        <Columns>
                            <asp:TemplateField HeaderText="Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="File Length">
                                <ItemTemplate>
                                    <asp:Label ID="lblLen" runat="server" Text='<%#Eval("Length")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="File Extention">
                                <ItemTemplate>
                                    <asp:Label ID="lblFileType" runat="server" Text='<%#Eval("Extension")%>'>
                                    </asp:Label></ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Creation Date & Time">
                                <ItemTemplate>
                                    <asp:Label ID="lblDateTime" runat="server" Text='<%#Eval("CreationTime")%>'>
                                    </asp:Label></ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

                    <p>
                        <asp:Label Text="" ID="lblMsg" runat="server"></asp:Label></p>

CS 代码:

protected void Folder_Treeview_SelectedItemChanged(object sender, EventArgs e)
    {
        TreeNode node = this.Folder_Treeview.SelectedNode;
        SetFolderPath(node);
    }

    public void SetFolderPath(TreeNode node)
    {        
        Session["ParentFolderId"] = node;
        //  System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));
        string CurrNode = node.Text;
        string separator = "\";
        Folder_Treeview.PathSeparator = Convert.ToChar(separator);

        while (node.Parent != null)
        {
            CurrNode = node.Parent.Text + this.Folder_Treeview.PathSeparator + CurrNode;
            node = node.Parent;
        }
        ViewState["Folder"] = CurrNode;
        ViewState["FileType"] = "All";
        GetFilesFromFolder();
    }

private void GetFilesFromFolder()
    {
        // GET A LIST OF FILES FROM A SPECIFILED FOLDER.
        DirectoryInfo objDir = new DirectoryInfo(Server.MapPath((string)ViewState["Folder"]));

        FileInfo[] listfiles = objDir.GetFiles("*." + ((string)ViewState["FileType"] != "All" ?
            ViewState["FileType"] : "*"));

        if (listfiles.Length > 0)
        {
            // BIND THE LIST OF FILES (IF ANY) WITH GRIDVIEW.
            GridView1.Visible = true;
            GridView1.DataSource = listfiles;
            GridView1.DataBind();

            lblMsg.Text = listfiles.Length + " files found";
        }
        else
        {
            GridView1.Visible = false;
            lblMsg.Text = "No files found";
        }
    }

OnSelectedNodeChanged 方法在 node 单击时调用,所有值设置正确但从未反映。

请帮忙。

这是您的更新面板。节点事件在 UpdatePanel 中发起,因此只有 Update 面板会在回发后得到更新。请记住,整个页面生命周期都会发生,因此 gridview 确实会进行数据绑定,但只会刷新 UpdatePanel 中的内容。

您的选择:

  1. Folder_Treeview 添加为回传触发器 - 或 -
  2. 完全摆脱 UpdatePanel - 或 -
  3. 将 Gridview 移到 UpdatePanel ContentTemplate 中

还有一个TreeView is one of several server controls that may not be compatible with an UpdatePanel:

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:

  • TreeView control under several conditions. One is when callbacks are enabled that are not part of an asynchronous postback. Another is when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. Another is when the EnableClientScript property is false (the default is true). Another is if you change the value of the EnableClientScript property between asynchronous postbacks. For more information, see TreeView Web Server Control Overview.

  • Menu control when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. For more information, see Menu Control Overview.

  • FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.

  • GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

  • Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.

  • The Substitution control.