1

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.

4

3 回答 3

1

您不应在 NavigateURL 中使用 # in 并写入 <%。

遵循以下示例

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

于 2010-12-03T18:30:00.343 回答
0

与其把所有的工作都放在前端,不如把 URL 构建放在代码后面呢?

在前端:

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

在代码隐藏中:

public string BuildURL(string a, string b, string c){ /* 使用字符串生成器连接字符串并返回 */}

您仍然必须绑定控件 每次绑定链接时,都会使用适当的值调用该方法,并将完整的字符串返回给 NavigateURL。

于 2010-12-03T20:20:49.803 回答
0

我这样做

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

于 2011-05-10T13:43:25.943 回答