How to dynamically create new hyperlinks in ASP.NET?

I will explain what I am 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. I can do this using PHP by mixing PHP code and HTML code. This allows me to generate actual HTML tags dynamically.

In ASP.NET, I did not find a way to replicate this functionality.

I thought about using a loop in the code for something like the Init () function to create an array of new () objects, set their attributes and hope that it gets passed to the aspx file, but it doesn't work.

How to do it?

+3
source share
4

ASP.Net :

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

, LinkButtons ASP.Net

, , , HyperLink, , - Controls, span - .

LinkButtons . Repeater, , . (, , - .)

Repeater, , , , .

+4

asp.net :

  • , , <% %> aspx, , php classic asp. .
  • , , , . , .
  • , visible false "" , . # 2 - asp.net.
  • -, - . β€” , , , , . , .
+4

If you want to generate controls dynamically and should not have these PostBack on the server (i.e. when the control is pressed / changed, it will return to the same page) - you can use the controls in System.Web.UI.HtmlControls .

eg,

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 and replace the fixed value with what comes from the database / object.

+4
source

Source: https://habr.com/ru/post/1709725/


All Articles