2012-02-20 67 views
-1

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

在Google上搜索时说,我需要使用!IsPostBack或TreeView的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);"); 
    } 

回答

1

据我了解,你应该叫AdNewNode()按钮被点击时。

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

编辑

这条线:

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

你应该把它里面的预渲染,但不能点击。

+0

问题是,如果我把这个函数放在按钮点击中,那么整个节点将按照用户单击创建单个新节点的次数创建。另外,我不知道为什么我需要点击两次按钮才能使其工作。 – SpcCode 2012-02-20 18:00:28

+0

请参阅我的编辑。 – Tengiz 2012-02-20 20:12:04

+0

嗨,我之前用过这个看到你的文章:

Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "targetWin = window.open('NewFolderPopup.aspx', 'Add Folder', 'toolbar=0, location=0, directories=0, status=0, resizable= 0, menubar=0, copyhistory=no, width=460, height=150'); targetWin.moveTo((screen.height/2) - (150/ 2), (screen.width/2) - (460/2));", true);
然后,我做了你所提及的工作。谢谢! – SpcCode 2012-02-21 02:51:50