Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

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

 

 

code behind page

        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

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

 

 

code behind page

      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?!?

Posted

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:

<div id="divLinks" runat="server"></div>

From there, just do the following:

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...