如何在运行时在网页中放置超链接字段?

发布于 2024-09-12 03:55:52 字数 662 浏览 5 评论 0原文

我正在尝试在超链接中显示文件夹的内容。我也在使用母版页。超链接不会显示在内容页面中。为此该怎么办?

我知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

囍孤女 2024-09-19 03:55:52

您无法将其直接添加到 Page.Controls 中。您必须将其添加到页面上的 ContentPlaceHolder 中。

You can't add it directly to Page.Controls. You have to add it to the ContentPlaceHolder on the page.

蓝眼睛不忧郁 2024-09-19 03:55:52

您是否考虑过使用 asp:Repeater 控件并将文件直接绑定到它,而不是动态创建控件(这相当混乱且容易出错)?类似于:

<asp:Repeater ID="RepeaterFiles" runat="server">
    <ItemTemplate>
        <asp:HyperLink runat="server" Text='<%# Container.DataItem %>' 
            NavigateUrl='<%# String.Format("downloading.aspx?file={0}", Container.DataItem)%>' />
        <br />
    </ItemTemplate>
</asp:Repeater>

在代码隐藏中:

DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
RepeaterFiles.DataSource = di.GetFiles();
RepeaterFiles.DataBind();

这样您就可以使用声明性标记来控制布局并将逻辑保留在代码隐藏中。

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:

<asp:Repeater ID="RepeaterFiles" runat="server">
    <ItemTemplate>
        <asp:HyperLink runat="server" Text='<%# Container.DataItem %>' 
            NavigateUrl='<%# String.Format("downloading.aspx?file={0}", Container.DataItem)%>' />
        <br />
    </ItemTemplate>
</asp:Repeater>

and in code behind:

DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
RepeaterFiles.DataSource = di.GetFiles();
RepeaterFiles.DataBind();

That way you can use declarative mark-up to control layout and keep the logic in your code-behind.

寄居者 2024-09-19 03:55:52

在页面上放置一个 PlaceHolder 控件:

<asp:PlaceHolder runat="server" ID="ph" />

在后面的代码中这样写:

HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
ph.Controls.Add(HL);
ph.Controls.Add(new Literal { Text = "<br/>"});

我在最后一行使用新的 C# 3 功能来设置 Text 属性。

Put a PlaceHolder control on your page:

<asp:PlaceHolder runat="server" ID="ph" />

In your code behind write like:

HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
ph.Controls.Add(HL);
ph.Controls.Add(new Literal { Text = "<br/>"});

I'm making use of the newly C# 3 feature on that last line to set the Text property.

北音执念 2024-09-19 03:55:52

您是否使用调试器单步执行循环以验证它是否至少处理了一个文件?

您可以在页面上放置一个列表控件,然后在列表项中添加每个链接,而不是将链接添加到 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.

空心↖ 2024-09-19 03:55:52

在页面的内容区域中创建面板或标签,并将超链接添加到面板的控件集合中。

(单步执行代码来检查 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文