slimshady Posted July 4, 2006 Posted July 4, 2006 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?!? Quote
Administrators PlausiblyDamp Posted July 4, 2006 Administrators Posted July 4, 2006 Try Dim s As String = "" If vDataTable.Rows.Count > 0 Then For Each vRow In vDataTable.Rows s = s & " " Next lblCompany.Text = s End If Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Gill Bates Posted July 4, 2006 Posted July 4, 2006 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.) Quote
slimshady Posted July 5, 2006 Author Posted July 5, 2006 I tried Gill's solution and it's working perfectly! :) Thanks for the help!!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.