C# 如何在 ASP.NET 中动态创建新的超链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/947973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:01:29  来源:igfitidea点击:

How do I dynamically create new Hyperlinks in ASP.NET?

c#.netasp.netvisual-studiohyperlink

提问by chustar

I'll explain what I'm trying to achieve:

我将解释我想要实现的目标:

I want to have a situation where I can create as many controls as I want by creating them in a loop in the code behind. I can do this while using PHP, by mixing the PHP code and the HTML code. This allows me to generate actual HTML tags dynamically.

我想要一种情况,通过在后面的代码中的循环中创建它们,我可以创建任意数量的控件。我可以在使用 PHP 时通过混合 PHP 代码和 HTML 代码来做到这一点。这允许我动态生成实际的 HTML 标签。

In ASP.NET, I haven't found a way to replicate this functionality.

在 ASP.NET 中,我还没有找到复制此功能的方法。

I've thought about using a loop in the code behind on something like the Init() function to create an array of new() objects, setting their attributes and hoping it is passed into the aspx file, but it didn't work.

我曾考虑在诸如 Init() 函数之类的代码中使用一个循环来创建一个 new() 对象数组,设置它们的属性并希望它被传递到 aspx 文件中,但它没有用。

How do I do this?

我该怎么做呢?

采纳答案by Muhammad Akhtar

If you want to creat Dynamically ASP.Net Hyperlink control You can simply do this:

如果您想动态创建 ASP.Net 超链接控件,您可以简单地执行以下操作:

HyperLink hyp = new HyperLink();
    hyp.ID = "hypABD";
    hyp.NavigateUrl = "";
    Page.Controls.Add(hyp);

回答by Spencer Ruport

Well, keep in mind there's a difference between Hyperlinks and LinkButtons in ASP.Net

好吧,请记住 ASP.Net 中的超链接和 LinkBut​​ton 之间是有区别的

If you just want Hyperlinks, you can create as many as you want by creating a HyperLink object, creating new objects and then adding them to some control's Controls collection like panel or span or something.

如果您只需要超链接,则可以通过创建超链接对象、创建新对象然后将它们添加到某些控件的控件集合(如面板或跨度等)来创建任意数量的超链接。

LinkButtons are a little different. You have to use a Repeater control if you want to create a link that posts back to the server. (Well, you don't have to but the alternative is a hack job.)

LinkBut​​tons 有点不同。如果要创建回发到服务器的链接,则必须使用中继器控件。(好吧,您不必这样做,但另一种选择是黑客工作。)

Hyperlinks can be created by using a Repeater control as well and, if possible is the method I would recommend.

也可以使用中继器控件创建超链接,如果可能,我会推荐这种方法。

回答by Joel Coehoorn

There are lotsof ways to do this in asp.net:

在asp.net中有很多方法可以做到这一点:

  1. If you really wanted to, you could put a loop using <% %>tags directly into the aspx markup, just like with php or classic asp. This is notrecommended.
  2. You can put a placeholder or panel control on your form, create as many hyperlink controls or anchor tags as you want in your code behind and add them to the placeholder's controls collection. This is a little better, but still not optimal.
  3. You can put a reasonable number of hyperlink controls on the page, set their visibleproperty to false by default, and "enable" the ones you need. This is preferred to #2 because of some oddities with asp.net page lifecycle.
  4. These links come from somewhere, and that somewhere is usually a database or other reasonable datasource. So make it data driven — bind that datasource to a control like a repeater, gridview, details view, or similar. This is probably your best option.
  1. 如果您真的愿意,可以将使用<% %>标签的循环直接放入 aspx 标记中,就像使用 php 或经典 asp 一样。这是推荐的。
  2. 您可以在表单上放置占位符或面板控件,根据需要在后面的代码中创建任意数量的超链接控件或锚标记,并将它们添加到占位符的控件集合中。这稍微好一点,但仍然不是最佳的。
  3. 您可以在页面上放置合理数量的超链接控件visible,默认情况下将它们的属性设置为 false,并“启用”您需要的那些。由于asp.net页面生命周期的一些奇怪之处,这比#2更受欢迎。
  4. 这些链接来自某个地方,而那个地方通常是一个数据库或其他合理的数据源。所以让它成为数据驱动的——将该数据源绑定到一个控件,如转发器、网格视图、详细信息视图或类似的控件。这可能是您最好的选择。

回答by shahkalpesh

If you want to generate controls dynamically & don't need to have those PostBack to the server (i.e. when a control is clicked/changed, it will come back to the same page) - you could use controls in System.Web.UI.HtmlControlsnamespace.

如果您想动态生成控件并且不需要将这些回发到服务器(即当单击/更改控件时,它将返回到同一页面) - 您可以使用System.Web.UI 中的控件。 HtmlControls命名空间。

e.g.

例如

HtmlAnchor link = new HtmlAnchor();
link.href = "www.stackoverflow.com";
Page.Controls.Add(link);

Hope this gives you enough background.

希望这能给你足够的背景。

EDIT: You can use the above code in a loop & replace the fixed value with what comes from a database/object.

编辑:您可以在循环中使用上述代码并用来自数据库/对象的内容替换固定值。