How do I create (more than one) dynamic hyperlinks

slimshady

Newcomer
Joined
Apr 18, 2006
Messages
8
I'm trying to create dynamic hyperlinks but it doesn't work properly. I've searched the net but can't find anything similar to what I'm trying to do...

The code should print a hyperlink per company that the user has permission to, ie if user has permission to three companies there should be three hyperlinks, one for each company. I only get 1 no mather how many I have permissions to! It seems it can't handle more than one link?

First I tried to create the hyperlink in the code behind

aspx page
Code:
<asp:Label ID="lblCompany" runat="server"></asp:Label>


code behind page
Code:
        Dim vDataTable As DataTable = Permissions.GetPermission(UserId)
        Dim vRow As DataRow
        If vDataTable.Rows.Count > 0 Then
            Dim s As String = ""
            For Each vRow In vDataTable.Rows
                s = s & "<br><asp:hyperlink NavigateUrl='Page.aspx?company=' & vRow.Item(2) Text='" & vRow.Item(2) & "' runat='server'></asp:HyperLink>"
            Next
            lblCompany.Text = s
        End If


And since that didn't work I tried this

aspx page
Code:
<asp:HyperLink ID="lnkCompany" runat="server"></asp:HyperLink>


code behind page
Code:
      Dim vDataTable As DataTable = Permissions.GetPermission(UserId)
        Dim vRow As DataRow
        If vDataTable.Rows.Count > 0 Then
            For Each vRow In vDataTable.Rows
                lnkCompany.Text = vRow.Item(2)
                lnkCompany.NavigateUrl = "Page.aspx?company=" & vRow.Item(2)
            Next
        End If

Needless to say that neither of them works, otherwise I wouldn't be turning to you guys :-\
How do I go about to get a link for every "vRow" in the datatable?!?
 
Try

Visual Basic:
Dim s As String = ""
If vDataTable.Rows.Count > 0 Then
                   For Each vRow In vDataTable.Rows
                s = s & "<br><asp:hyperlink NavigateUrl='Page.aspx?company=' & vRow.Item(2) Text='" & vRow.Item(2) & "' runat='server'></asp:HyperLink>"
            Next
            lblCompany.Text = s
        End If
 
I do not think that you can simply build up a server control declaration like that and then set it into the text for a label. It is not going to be ran through the preprocessor and therefore will not get translated into HTML.

Assume you have a div on your form:
Code:
<div id="divLinks" runat="server"></div>
From there, just do the following:
C#:
HyperLink link;

Literal literal;

for (int i = 0; i < 10; i++)
{
    link = new HyperLink();
    link.Text = "Link_" + i;
    link.NavigateUrl = "test.aspx?company=" + i;

    literal = new Literal();
    literal.Text = "<br>";

    divLinks.Controls.Add(link);
    divLinks.Controls.Add(literal);
}
(This is C# obviously, but you get the idea. Just declare instances of the server controls instead.)
 
Back
Top