• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

Substitution on ASP.NET page of asp control property

In ASP we used to be able to do this ... <a href="/help/<%=MyHelpPage%>.asp">hyperlink</a>
and ... <%=MyHelpPage%> ... got substituted with the string value.

So I tried something similar with ASP hyperlink control ...
<asp:HyperLink NavigateUrl="~/help/<%=MyHelpPage%>.aspx" ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>

Above is WRONGLY translated to HTML like this ...
href="~/help/&lt;%=MyHelpPage%>/.aspx"

Bottom line ... it don't work! Probably because it's being translated at the server. OK, but I've got hundreds of Help Hyperlinks. I do NOT want to MANUALLY set the page name on all of them.

In ASP.NET ...  how else can I accomplish the same thing?
0
volking
Asked:
volking
2 Solutions
 
QPRCommented:
Try
<asp:HyperLink NavigateUrl<%# eval('MyHelpPage')%>.aspx" ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>
0
 
RineauCommented:
You should use the following syntax:

<asp:HyperLink NavigateUrl='<%# "~/help/ " & Eval("MyHelpPage") & ".aspx" %>' ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>

Hope this helps!
0
 
volkingAuthor Commented:
--------------------------
The Problem
--------------------------

Error is:
Server tags cannot contain <% ... %> constructs

Problem deals with the order in which .NET does things.

In ASP, we used to be able to do this ...
    <a href="/help/<%=MyHelpPage%>.asp">hyperlink</a>
and ...
    <%=MyHelpPage%> ... got substituted with the string value.

So I tried something similar with ASP hyperlink control ...
<asp:HyperLink NavigateUrl="~/help/<%=MyHelpPage%>.aspx" ID="HyperLink1" runat="server">hyperlink</asp:HyperLink>

Above is WRONGLY translated to HTML like this ...
href="~/help/&lt;%=MyHelpPage%>/.aspx"

and every thing else I tried ended with: Server tags cannot contain <% ... %> constructs
Bottom line ... it don't work!

So ... how to accomplish similar funcationality in ASP.NET

---------------------------
The Solution (in VB)
---------------------------

Code in markup ...

    <asp:HyperLink NavigateUrl='<%#mHelpPageURL & "bookmark" %>' ID="HyperLink1" runat="server">
    <img src="images/myicon.GIF" border="0" />
    </asp:HyperLink>

Code in Code Behind

    Partial Class mywebpage
        Public mHelpPageURL As String = "~/help/use1_help.aspx#"
        Protected Sub Page_Load(...)
            Me.Page.DataBind()
        End Sub
    End Class

Because of the order in which ASP.NET processes things, the solution needs DATA BINDING to work. The # sign in the asp <%...%> tells the compiler that this is a DataBound value. The exact syntax between the <% and %> depends on the language being used (VB or C#). In the code behind, notice Page.DataBind(). That's the call which does the work.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now