ASP.NET 4 ACCESS DATA TO APPLY TO NavigateUrl

2019-07-26 06:38发布

问题:

I am currently learning .net and have come to a brick wall with trying to implement url rounting.

I have most of it working fine, but I am trying to generate hyperlinks from information in my database.

I am getting the data out fine using:

    'portfolio navigation data
    Dim rdrPortfolioNav As SqlDataReader

    Dim cmdPortfolioNav As SqlCommand = New SqlCommand()
    cmdPortfolioNav.CommandText = "SELECT TOP 6 [id], [date], [client], [category], [title], [body], [website], [navimage], [navdesc] FROM [portfolio] ORDER BY [date] DESC"
    cmdPortfolioNav.CommandType = CommandType.Text
    cmdPortfolioNav.Connection = boomSQL

    cmdPortfolioNav.Connection.Open()
    rdrPortfolioNav = cmdPortfolioNav.ExecuteReader(CommandBehavior.CloseConnection)

    lvPortfolioNav.DataSource = rdrPortfolioNav
    lvPortfolioNav.DataBind()

    cmdPortfolioNav.Dispose()

On the front end i can access the data and show all records using:

<asp:ListView ID="lvPortfolioNav" runat="server">
<ItemTemplate>
    <div class="work">
        <asp:HyperLink runat="server" NavigateUrl="portfolio/<%# DataBinder.Eval(Container.DataItem, &quot;id&quot;)%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, &quot;category&quot;)) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, &quot;title&quot;)) %>" ToolTip=""><span class="title"><%# DataBinder.Eval(Container.DataItem, "title")%></span></asp:HyperLink>
        <asp:Image runat="server" ImageUrl="<%# DataBinder.Eval(Container.DataItem, &quot;navimage&quot;)%>" AlternateText="<%# DataBinder.Eval(Container.DataItem, &quot;client&quot;)%>" ToolTip="<%# DataBinder.Eval(Container.DataItem, &quot;client&quot;)%>" />
        <span class="desc"><%# DataBinder.Eval(Container.DataItem, "navdesc")%></span> </div>
</ItemTemplate>

The problem is this line:

<asp:HyperLink runat="server" NavigateUrl="portfolio/<%# DataBinder.Eval(Container.DataItem, &quot;id&quot;)%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, &quot;category&quot;)) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, &quot;title&quot;)) %>" ToolTip=""><span class="title"><%# DataBinder.Eval(Container.DataItem, "title")%></span></asp:HyperLink>

it won't get the values from the database and in the html the link literally comes out as:

<a href="../../portfolio/%3C%25#%20DataBinder.Eval(Container.DataItem,%20%22id%22)%25%3E/%3C%25%23%20FormatLinks(DataBinder.Eval(Container.DataItem,%20%22category%22))%20%25%3E/%3C%25%23%20FormatLinks(DataBinder.Eval(Container.DataItem,%20%22title%22))%20%25%3E"><span class="title">Kingston Bagpuize House Website</span></a>

the same thing works fine for the ImageUrl so not sure what i'm doing wrong.

I know you can do something in the backend code to generate the urls but i can't for the life of me find anything on the internet.....help would be very much appreciated.

Thanks.

J.

回答1:

You should not use # in and write <% in NavigateURL.

Follow the following example

http://www.extremeexperts.com/Net/FAQ/PassingMulitpleParameterinURLLink.aspx



回答2:

Rather than doing all the work in the front end, why not put the URL building in the code behind?

In front end:

NavigateUrl='<%# this.BuildURL(DataBinder.Eval(Container.DataItem, "id"),DataBinder.Eval(Container.DataItem, "category"), DataBinder.Eval(Container.DataItem, "title"))'

In codebehind:

public string BuildURL(string a, string b, string c){ /* Concatenate the string using string builder and return */}

You will still have to bind the control Each time the link is bound the method will be called with the appropriate values and the completed string will be returned to NavigateURL.



回答3:

I do it like this

NavigateUrl= '<%#"~/ProductList.aspx?ITEMSUBCATID=" + DataBinder.Eval(Container.DataItem, "ITEMSUBCATID")%>'