如何在运行时在网页中放置超链接字段?
我正在尝试在超链接中显示文件夹的内容。我也在使用母版页。超链接不会显示在内容页面中。为此该怎么办?
我知道在 Windows 窗体中我们可以使用像 TextBox.Location=新点(100,100);
但是如何在网页中执行...请有人建议我..
我在 page_load 中的编码是
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
Page.Controls.Add(HL);
Page.Controls.Add(new LiteralControl("<br/>"));
}
}
I am trying to display contents of a folder in a hyperlink. I am using masterpage also. The hyperlinks are not shown into the content page. what to do for that?
I know in windows forms we can use like
TextBox.Location=new Point(100,100);
But how to do in web page...please anybody suggest me..
my coding in page_load is
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
Page.Controls.Add(HL);
Page.Controls.Add(new LiteralControl("<br/>"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法将其直接添加到
Page.Controls
中。您必须将其添加到页面上的ContentPlaceHolder
中。You can't add it directly to
Page.Controls
. You have to add it to theContentPlaceHolder
on the page.您是否考虑过使用
asp:Repeater
控件并将文件直接绑定到它,而不是动态创建控件(这相当混乱且容易出错)?类似于:在代码隐藏中:
这样您就可以使用声明性标记来控制布局并将逻辑保留在代码隐藏中。
Instead of dynamically creating controls, which is rather messy and error-prone, have you considered using an
asp:Repeater
control and binding the files directly to it? Something like:and in code behind:
That way you can use declarative mark-up to control layout and keep the logic in your code-behind.
在页面上放置一个 PlaceHolder 控件:
在后面的代码中这样写:
我在最后一行使用新的 C# 3 功能来设置 Text 属性。
Put a PlaceHolder control on your page:
In your code behind write like:
I'm making use of the newly C# 3 feature on that last line to set the Text property.
您是否使用调试器单步执行循环以验证它是否至少处理了一个文件?
您可以在页面上放置一个列表控件,然后在列表项中添加每个链接,而不是将链接添加到
Page.Controls
。然后您就可以准确地知道它们应该出现在页面上的哪个位置。Have you used the debugger to step through the loop to verify that it processes at least one file?
Instead of adding the links to
Page.Controls
, you could put a list control on the page, and then add each link within a list item. Then you'd know precisely where on the page they should appear.在页面的内容区域中创建面板或标签,并将超链接添加到面板的控件集合中。
(单步执行代码来检查 IIS 应用程序是否确实枚举了目录中的任何文件也会有所帮助。)
Create a Panel or a Label in the Page's Content area, and add your HyperLinks into the Controls collection of the Panel.
(Stepping through the code to check whether the IIS App actually enumerates any files in the directory would help too.)