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



Question:

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.

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

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.

How do I do this?


Solution:1

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

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


Solution:2

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

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.)

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


Solution:3

There are lots of ways to do this in 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 not recommended.
  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 visible property 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.


Solution:4

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.HtmlControls namespace.

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »