单击按钮后 TreeView 不断添加内容

发布于 2025-01-07 12:31:27 字数 3617 浏览 1 评论 0原文

我在一个 ASP 网站工作。我正在使用 SQL Server 中的某些表动态地将数据插入到 TreeView 中。我想添加一个功能,它使用一个按钮来创建一个新的子节点。 问题是,当我单击按钮时,页面似乎“刷新”,然后它再次添加页面加载时已填充的内容。 我已经找到了解决方案,使用 PreRender 中的 IsPostBack但是,我需要点击两次按钮才能打开一个弹出窗口来输入子节点的名称。

在Google上搜索,说我需要使用!IsPostBack,或者PreRender方法树视图。然而,这些方法都不适合我。下面是我的代码,

<div id="content">
<div class="post">
<h1 class="title"> <asp:Label ID="lblTitle" runat="server" Text="Documents"></asp:Label></h1>

    <div class="entry" >

     <!-- Center the pop window in the middle -->
        <script type="text/javascript">
            function PopupCenter(pageURL, title, w, h) {
                var left = (screen.width / 2) - (w / 2);
                var top = (screen.height / 2) - (h / 2);
                var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            } 
        </script>

        <a href="javascript:void(0);" onclick="PopupCenter('NewFolderPopup.aspx', 'Add Document',340,185);">Click Here for Upload</a>
        <font size="4">

        <asp:TreeView ID="TreeViewDocuments" runat="server" ExpandDepth="0" 
            ImageSet="Simple" Visible="False" onprerender="TreeView_PreRender">
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <LeafNodeStyle NodeSpacing="10px" />
            <Nodes>
                <asp:TreeNode Text="System" Value="Systems" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
                <asp:TreeNode Text="Document" Value="Documents" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
            </Nodes>
            <NodeStyle Font-Names="Tahoma" Font-Size="Medium" ForeColor="Black" 
                HorizontalPadding="0px" NodeSpacing="7px" VerticalPadding="0px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>

            <asp:Button ID="btnNewFolder" runat="server" Text="New Folder" 
            onclick="btnNewFolder_Click" UseSubmitBehavior="False"/>
            <asp:Button ID="btnRenameFolder" runat="server" Text="Rename Folder" />
            <asp:Button ID="btnDeleteFolder" runat="server" Text="Delete Folder" />
   </font></div>

</div>

代码背后,

        protected void Page_Load(object sender, EventArgs e)
    {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) {
                TreeViewDocuments.Visible = false;
                lblTitle.Text = "You need to be logged in.";
            }
            else
                TreeViewDocuments.Visible = true;
    }
    protected void TreeView_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           AddNewNode();
        }
    }
    // Some Code to pop 
     protected void AddNewNode()
     {
      ...
     }
    .
    .
    .
    protected void btnNewFolder_Click(object sender, EventArgs e)
    {
        if (TreeViewDocuments.Nodes[0].ChildNodes[0].Checked)
        btnNewFolder.Attributes.Add("onclick", "PopupCenter('NewFolderPopup.aspx', 'Add Folder',340,125);");
    }

I am working in a ASP website. I am inserting data to a TreeView dynamically using some table from SQL Server. I want to add a feature, which use a button to create a new child-node.
The problem is that when I click the button the page seems to 'refresh' and then it add again the content which was already populated when the page loaded. I have get the solution for this, using the IsPostBack inside the PreRender. However, I need to click the button twice to open a popup window to enter the name of the child-node.

Searching over Google, said that I need to use the !IsPostBack, or the PreRender method for the TreeView. However, neither of these methods worked for me. Below is my code,

<div id="content">
<div class="post">
<h1 class="title"> <asp:Label ID="lblTitle" runat="server" Text="Documents"></asp:Label></h1>

    <div class="entry" >

     <!-- Center the pop window in the middle -->
        <script type="text/javascript">
            function PopupCenter(pageURL, title, w, h) {
                var left = (screen.width / 2) - (w / 2);
                var top = (screen.height / 2) - (h / 2);
                var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            } 
        </script>

        <a href="javascript:void(0);" onclick="PopupCenter('NewFolderPopup.aspx', 'Add Document',340,185);">Click Here for Upload</a>
        <font size="4">

        <asp:TreeView ID="TreeViewDocuments" runat="server" ExpandDepth="0" 
            ImageSet="Simple" Visible="False" onprerender="TreeView_PreRender">
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <LeafNodeStyle NodeSpacing="10px" />
            <Nodes>
                <asp:TreeNode Text="System" Value="Systems" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
                <asp:TreeNode Text="Document" Value="Documents" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
            </Nodes>
            <NodeStyle Font-Names="Tahoma" Font-Size="Medium" ForeColor="Black" 
                HorizontalPadding="0px" NodeSpacing="7px" VerticalPadding="0px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>

            <asp:Button ID="btnNewFolder" runat="server" Text="New Folder" 
            onclick="btnNewFolder_Click" UseSubmitBehavior="False"/>
            <asp:Button ID="btnRenameFolder" runat="server" Text="Rename Folder" />
            <asp:Button ID="btnDeleteFolder" runat="server" Text="Delete Folder" />
   </font></div>

</div>

Code behind,

        protected void Page_Load(object sender, EventArgs e)
    {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) {
                TreeViewDocuments.Visible = false;
                lblTitle.Text = "You need to be logged in.";
            }
            else
                TreeViewDocuments.Visible = true;
    }
    protected void TreeView_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           AddNewNode();
        }
    }
    // Some Code to pop 
     protected void AddNewNode()
     {
      ...
     }
    .
    .
    .
    protected void btnNewFolder_Click(object sender, EventArgs e)
    {
        if (TreeViewDocuments.Nodes[0].ChildNodes[0].Checked)
        btnNewFolder.Attributes.Add("onclick", "PopupCenter('NewFolderPopup.aspx', 'Add Folder',340,125);");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-01-14 12:31:27

据我了解,您应该在单击按钮时调用 AdNewNode() 。

在您的情况下,当单击按钮时,会发生刷新并一次又一次添加新行。但即使没有点击它也会添加,不是吗?

编辑

这一行:

btnNewFolder.Attributes.Add("onclick", "PopupCenter('NewFolderPopup.aspx', 'Add Folder',340,125);"); 

您应该将其放在预渲染中,但不要单击。

As I understand you should call AdNewNode() when button is clicked.

In your case, when button is clicked, the refresh happens and the new row is added again and again. But it is added even without click, no?

EDIT:

This line:

btnNewFolder.Attributes.Add("onclick", "PopupCenter('NewFolderPopup.aspx', 'Add Folder',340,125);"); 

You should put it inside the pre-render but not click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文