2012-02-20 44 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