-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);");
}
問題是,如果我把這個函數放在按鈕點擊中,那麼整個節點將按照用戶單擊創建單個新節點的次數創建。另外,我不知道爲什麼我需要點擊兩次按鈕才能使其工作。 – SpcCode 2012-02-20 18:00:28
請參閱我的編輯。 – Tengiz 2012-02-20 20:12:04
嗨,我之前用過這個看到你的文章:
然後,我做了你所提及的工作。謝謝! – SpcCode 2012-02-21 02:51:50