1

Topic: Dynamic creation of a hyperlink in masterpage

In  after authorization passage the falling out menu with links is formed.
I try to add to them the with the text "My list" and href = address "~/MyLists? UserID=XYZ".

<LoggedInTemplate>
<ul runat = "server" class = "nav navbar-nav navbar-right" id = "LogPanel">
<li> <a runat = "server" href = "~/MyLists? UserID=XYZ"> My list </a> </li>
<li> <a runat = "server" href = "~/Account/Manage" title = "Manage your account"> <%: Context. User. Identity. GetUserName () %> </a> </li>
<li> <asp:LoginStatus runat = "server" ID = "itmLogout" LogoutAction = "Redirect" LogoutText = "Output" LogoutPageUrl = "~ /" OnLoggingOut = "Unnamed_LoggingOut"/>
</li>
</ul>
</LoggedInTemplate>

Instead of XYZ there should be number of the user which I can receive by means of a tag

<%: Context. Session ("UserID").ToString %>

But how to me to interpose the given tag into this code? As a result to receive, for example

<li> <a runat = "server" href = "~/MyLists? UserID=55"> My list </a> </li>

As tried, produces an error.

2

Re: Dynamic creation of a hyperlink in masterpage

Made through codebehind.
Replaced in site.master the link:

 <a runat = "server" href = "~/MyLists? UserID=XYZ"> My list </a> 
<asp:LoginView runat = "server" id = "LogoutPanel" ViewStateMode = "Disabled">
...
<LoggedInTemplate>
<ul runat = "server" class = "nav navbar-nav navbar-right" id = "LogPanel">
<li> <a runat = "server" href = "~/MyLists? UserID=XYZ"> My list </a> </li>
</ul>
</LoggedInTemplate>
</asp:LoginView>

On:

 <asp:HyperLink ID = "hlMyLists" runat = "server"> My list </asp:HyperLink> 
<asp:LoginView runat = "server" id = "LogoutPanel" ViewStateMode = "Disabled">
...
<LoggedInTemplate>
<ul runat = "server" class = "nav navbar-nav navbar-right" id = "LogPanel">
<li> <asp:HyperLink ID = "hlMyLists" runat = "server"> My list </asp:HyperLink> </li>
</ul>
</LoggedInTemplate>
</asp:LoginView>

And in Site.master.vb registered

Protected Sub Page_Load (sender As Object, e As EventArgs)
Dim iUserID As Int16
iUserID = Request. QueryString ("UserID")
If iUserID> 0 Then
Dim lp As Control = FindControl ("LogoutPanel")
Dim hl1 As HyperLink = FindControlRecursive (lp, "hlMyLists")
hl1.NavigateUrl = "~/MyLists? UserID =" AND iUserID.ToString
End If
End Sub
Private Function FindControlRecursive (ByVal rootControl As Control, ByVal controlID As String) As Control
If rootControl. ID = controlID Then
Return rootControl
End If
For Each controlToSearch As Control In rootControl. Controls
Dim controlToReturn As Control =
FindControlRecursive (controlToSearch, controlID)
If controlToReturn IsNot Nothing Then
Return controlToReturn
End If
Next
Return Nothing
End Function

Now all works as it is necessary.
After authorization in the falling out menu of the panel there is a link to page "My list" corresponding UserID

<li> <an id = "LogoutPanel_hlMyLists" href = "MyLists? UserID=33"> the List of lessons </a> </li>

But, someone can prompts a method easier, for example with the same <%: %> ...

3

Re: Dynamic creation of a hyperlink in masterpage

Malyav;
It is not necessary to transfer ID the current user in page parameters, it is necessary to take it from a context of the current user in HttpContext. User.