asp.net - Hyperlink not working
protected void Button1_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\Documents and Settings\Admin\Desktop\New Folder\"+TextBox1.Text);
foreach (System.IO.FileInfo file in dir.GetFiles())
{
HyperLink h = new HyperLink();
h.NavigateUrl = "file:///c:/Documents and Settings/Admin/Desktop/New Folder/" + TextBox1.Text + "/" + file.Name;
h.Text = file.Name;
PlaceHolder1.Controls.Add(h);
}
}
On execution of this code hyperlinks get generated but they are not wo开发者_C百科rking. nothing happens when i click on them.
Please help.
In ASP.NET hyperlinks must be URLs not a folder on the computer.
If your file is in your site try Sever.MapPath
This is due to security restrictions in the browser. If you generate a "file://
" link, it is relative to the user's file system.
Theoretically, if browsers allowed these types of links, attackers could discover information about a user's file system remotely. Thus, modern browsers do not allow this type of link.
Unfortunately this is not very well documented, and most browsers allow the links and just drop the behaviour - so nothing happens when you click them. There aren't any good workarounds either.
See my question here for further discussion.
Check out this question. The solution may work for you too.
I have found that (in the context of redirecting to another one of your own web pages) using this: compontent.NavigateUrl = "~/page.aspx";
instead of this:
compontent.NavigateUrl = server.MapPath("path");
worked for me. The server.MapPath created a File:// link so that it was not allowed by the browser.
Hope this helps someone, it won't work in some cases but it worked for my individual requirements.
精彩评论