用户单击html中的treeview节点时打开文件 [英] Open file when user clicks on treeview node in html

查看:121
本文介绍了用户单击html中的treeview节点时打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我单击树视图节点时,谁能建议我打开文件(.jpg,.xls,.pdf).

我的treeview节点已绑定到数据库.

我正在处理.htm页面.
使用javascript c#.

我尝试过类似的
a href ="my path" Treeviewnode/a

但是上面的内容对我不起作用...我们是否需要以某种不同的方式设置路径(例如双斜杠或其他方式).

谢谢

Hi
Can anyone suggest me to open my files(.jpg, .xls, .pdf) when i click treeview node.

My treeview node is binded to database.

I am working on .htm pages.
Using javascript, c#.

I tried something like
a href="my path" Treeviewnode /a

But above does not work for me...Do we need to set path in some different manner(like double slash or something else).

Thanks

推荐答案

你好Sushma,

我将要打开的文件的名称放在节点的相应值"字段中,并处理OnSelectedNodeChanged事件.

举个例子,我制作了一个TreeView并放置了一些节点:

Hello Sushma,

I would put the name of the files you want to open in the corresponding Value field of a node and handle the OnSelectedNodeChanged event.

To give you an example I made a TreeView and put in some nodes:

<asp:treeview id="TreeView1" runat="server" onselectednodechanged="TreeView1_SelectedNodeChanged" xmlns:asp="#unknown">
    <nodes>
        <asp:treenode text="Text Files" value="Text Files">
            <asp:treenode text="File A" value="fileA.txt"></asp:treenode>
            <asp:treenode text="File B" value="fileB.txt"></asp:treenode>
        </asp:treenode>
    </nodes>
</asp:treeview>



对于此快速示例,我没有将其绑定到数据源.基本上,您只需要将Value设置为在用节点填充TreeView时要打开的文件名(有关此信息,请参见TreeNodePopulate事件).默认情况下,值"字段与文本"字段相同,因此,如果显示的节点名称是您的实际文件名,则在这里无需执行任何操作.

现在,单击节点后,将触发SelectedNodeChanged事件,并按如下所示打开文件:



I did not bind it to a data source for this quick example. Basically you just need to set Value to the file name you want to open when you populate the TreeView with nodes (see the TreeNodePopulate event for that). By default the Value field is the same as the Text field, so if the displayed nodes names are your actual file name you don''t have to do anything here.

Now, when a node is clicked the SelectedNodeChanged event fires and you open the file like this:

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    string filename = Server.MapPath(TreeView1.SelectedValue);

    System.IO.FileInfo file = new System.IO.FileInfo(filename);

    if (file.Exists)
    {
        System.Diagnostics.Process.Start(file.FullName);
    }
}



请注意,Process.Start并不是打开文件的最复杂的方法,在这里我只是将其用于演示目的.在打开某个文件类型之前,应先检查是否需要为某种文件类型安装该程序,诸如此类,但是我相信您已经有了主意,希望这个答案对您有所帮助.

干杯!



Please note that Process.Start is not the most sophisticated way to open a file, I just use it for demonstration purposes here. You should do checks for the program you need installed for a certain file type before you open it and things like that, but I am sure you got the idea and I hope this answer helped you.

Cheers!


这篇关于用户单击html中的treeview节点时打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆