我在ASP网站上工作。我使用SQL Server中的一些表动态地将数据插入TreeView。我想添加一个功能,它使用一个按钮来创建一个新的子节点。 问题是,当我单击按钮时,页面似乎“刷新”,然后再次添加已加载页面时已填充的内容。 我已经使用PreRender内的IsPostBack 获得了解决方案。 但是,我需要点击两次按钮才能打开一个弹出窗口,输入子节点的名称。
通过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);");
}
答案 0 :(得分:1)
据我所知,你应该在点击按钮时调用AdNewNode()。
在您的情况下,单击按钮时,将进行刷新并反复添加新行。但即使没有点击它也会添加,没有?
修改强>:
这一行:
btnNewFolder.Attributes.Add("onclick", "PopupCenter('NewFolderPopup.aspx', 'Add Folder',340,125);");
你应该把它放在预渲染中但不要点击。