我按照本教程允许用户从他们的机器中选择一个文件夹: http://www.codeproject.com/Articles/21895/Directory-Browsing-in-ASP-Net
问题是每次我点击一个节点时,它都会调用postback,这会刷新页面。那么如何每次都停止回调?
<asp:TreeView ID="TreeView1" runat="server" Height="326px" ImageSet="XPFileExplorer"
NodeIndent="15" Width="292px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px" />
<LeafNodeStyle ImageUrl="../images/folder.gif" />
</asp:TreeView>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeNode onjParent = new TreeNode("C:\\", "C:\\");
onjParent.PopulateOnDemand = true;
TreeView1.Nodes.Add(onjParent);
TreeView1.CollapseAll();
}
error.Visible = false;
TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
}
void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
_browseTextBox.Text = TreeView1.SelectedValue;
}
void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Value.EndsWith("\\"))
{
AddNodes(e.Node.Value, e.Node);
}
}
private TreeNode AddNodes(string path, TreeNode parentNode)
{
FileList objList = new FileList(path, "*.*");
TreeNode node = new TreeNode(path, path);
for (int index = 0; index < objList.Directories.Length; index++)
{
string directory = objList.Directories[index];
TreeNode objChildNode = new TreeNode(directory, path + "\\" + directory + "\\");
objChildNode.PopulateOnDemand = true;
objChildNode.Target = "_blank";
parentNode.ChildNodes.Add(objChildNode);
}
foreach (string file in objList.Files)
{
TreeNode objChildNode = new TreeNode(file, path + "\\" + file);
parentNode.ChildNodes.Add(objChildNode);
}
return node;
}
protected void _browseButton_Click(object sender, ImageClickEventArgs e)
{
TreeView1.Nodes.Clear();
if (UpdateBrowseTextBoxWithSlash())
{
TreeNode onjParent = new TreeNode(_browseTextBox.Text, _browseTextBox.Text);
onjParent.PopulateOnDemand = true;
TreeView1.Nodes.Add(onjParent);
TreeView1.CollapseAll();
}
else
{
error.Visible = true;
error.Text = "Please Enter valid path";
}
}
private bool UpdateBrowseTextBoxWithSlash()
{
if (_browseTextBox.Text.Length != 0)
{
if (
-1 == _browseTextBox.Text.LastIndexOf(".") &&
!_browseTextBox.Text.Substring(_browseTextBox.Text.Length - 1, 1).Equals("/") &&
!_browseTextBox.Text.Substring(_browseTextBox.Text.Length - 1, 1).Equals("\\")
)
{
if (_browseTextBox.Text.Substring(0, 1).Equals("\\") || -1 != _browseTextBox.Text.IndexOf(":\\"))
_browseTextBox.Text += "\\";
else
_browseTextBox.Text += "/";
return System.IO.Directory.Exists(_browseTextBox.Text);
}
else if (_browseTextBox.Text.LastIndexOf(".") > 0)
{
return System.IO.File.Exists(_browseTextBox.Text);
}
}
return true;
}
答案 0 :(得分:0)
你看过Imperfect Solution To TreeView了吗?通过手动设置每个TreeNode的NavigateUrl和SelectAction,作者提供了一个有趣的想法,即在选择节点时省略回发。从第一眼看,它似乎合乎逻辑。唯一缺少的是它不是递归的,所以它一次只能处理一个节点级别。