如何在datalist中创建动态超链接 [英] How to create a dynamic hyperlink in datalist

查看:692
本文介绍了如何在datalist中创建动态超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据列表显示存储在SQL Server数据库中的新闻报道摘要。

 < asp: DataList ID =DL_NewsSummaryrunat =serverDataKeyField =newsItemId
DataSourceID =DS_NewsSummary>
< ItemTemplate>
< h3>
< asp:HyperLink ID =headlineLinkrunat =serverText ='<%#Eval(headline)%>'NavigateUrl =#/>
< / h3>
< asp:Label ID =dateLabelrunat =serverText ='<%#Eval(date)%>'/>
< br />
< asp:Label ID =introLabelrunat =serverText ='<%#Eval(intro)%>'/>
< hr />
< / ItemTemplate>
< / asp:DataList>

当用户点击标题超链接时,他们应该在单独的页面上看到完整的故事, news.aspx 。此页面将从查询字符串中获取 newsItemId ,并使用与该ID相关联的故事填充页面,例如 news.aspx?newsItemId = 1



然而,当我将navigateUrl字段更改为以下内容时,我得到一台服务器标签形成错误。

 < asp:HyperLink ID =headlineLinkrunat =serverText ='< %#Eval(标题)%>'NavigateUrl =news.aspx?newsItemId =<%#Eval(newsItemId)%> /> 

非常感谢任何帮助

试试这个:

  NavigateUrl ='<%#news.aspx?newsItemId = + Eval(newsItemId)%>'


I am using a datalist to display a summary of news stories stored in a SQL Server database.

<asp:DataList ID="DL_NewsSummary" runat="server" DataKeyField="newsItemId" 
    DataSourceID="DS_NewsSummary">
    <ItemTemplate>
        <h3>
            <asp:HyperLink ID="headlineLink" runat="server" Text = '<%# Eval("headline") %>' NavigateUrl="#" />
        </h3>
        <asp:Label ID="dateLabel" runat="server" Text='<%# Eval("date") %>' />
        <br />
        <asp:Label ID="introLabel" runat="server" Text='<%# Eval("intro") %>' />
        <hr />      
    </ItemTemplate>
</asp:DataList>

When the user clicks the headline hyperlink they should be taken to the full story on a separate page, news.aspx. This page will get the newsItemId from the querystring and populate the page with the story associated with that id e.g. news.aspx?newsItemId=1.

However when I change the navigateUrl field to the following I get a server tag not well formed error.

<asp:HyperLink ID="headlineLink" runat="server" Text = '<%# Eval("headline") %>' NavigateUrl="news.aspx?newsItemId=<%# Eval("newsItemId") %>" />

Any help is greatly appreciated

解决方案

Try this:

NavigateUrl='<%# "news.aspx?newsItemId=" + Eval("newsItemId") %>'

这篇关于如何在datalist中创建动态超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆