ASP.NET 4 访问数据应用于 NavigateUrl
我目前正在学习 .net,并在尝试实现 url 路由时遇到了困难。
我的大部分工作正常,但我试图从数据库中的信息生成超链接。
我可以使用以下方法很好地获取数据:
'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()
在前端,我可以使用以下方法访问数据并显示所有记录:
<asp:ListView ID="lvPortfolioNav" runat="server">
<ItemTemplate>
<div class="work">
<asp:HyperLink runat="server" NavigateUrl="portfolio/<%# DataBinder.Eval(Container.DataItem, "id")%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "category")) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "title")) %>" ToolTip=""><span class="title"><%# DataBinder.Eval(Container.DataItem, "title")%></span></asp:HyperLink>
<asp:Image runat="server" ImageUrl="<%# DataBinder.Eval(Container.DataItem, "navimage")%>" AlternateText="<%# DataBinder.Eval(Container.DataItem, "client")%>" ToolTip="<%# DataBinder.Eval(Container.DataItem, "client")%>" />
<span class="desc"><%# DataBinder.Eval(Container.DataItem, "navdesc")%></span> </div>
</ItemTemplate>
问题是这一行:
<asp:HyperLink runat="server" NavigateUrl="portfolio/<%# DataBinder.Eval(Container.DataItem, "id")%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "category")) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "title")) %>" ToolTip=""><span class="title"><%# DataBinder.Eval(Container.DataItem, "title")%></span></asp:HyperLink>
它不会从数据库中获取值,并且在 html 中,链接的字面意思是:
<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>
同样的事情对于 ImageUrl 来说工作正常,所以不确定我做错了什么。
我知道您可以在后端代码中执行某些操作来生成网址,但我一生都无法在互联网上找到任何内容......非常感谢您的帮助。
谢谢。
J。
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, "id")%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "category")) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "title")) %>" ToolTip=""><span class="title"><%# DataBinder.Eval(Container.DataItem, "title")%></span></asp:HyperLink>
<asp:Image runat="server" ImageUrl="<%# DataBinder.Eval(Container.DataItem, "navimage")%>" AlternateText="<%# DataBinder.Eval(Container.DataItem, "client")%>" ToolTip="<%# DataBinder.Eval(Container.DataItem, "client")%>" />
<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, "id")%>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "category")) %>/<%# FormatLinks(DataBinder.Eval(Container.DataItem, "title")) %>" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应在 NavigateURL 中使用 # 并写入 <%。
请遵循以下示例
http://www.extremeexperts.com/Net/FAQ/PassingMulitpleParameterinURLLink.aspx
You should not use # in and write <% in NavigateURL.
Follow the following example
http://www.extremeexperts.com/Net/FAQ/PassingMulitpleParameterinURLLink.aspx
与其在前端完成所有工作,为什么不将 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。
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.
我这样做
NavigateUrl= '<%#"~/ProductList.aspx?ITEMSUBCATID=" + DataBinder.Eval(Container.DataItem, "ITEMSUBCATID")%>'
I do it like this
NavigateUrl= '<%#"~/ProductList.aspx?ITEMSUBCATID=" + DataBinder.Eval(Container.DataItem, "ITEMSUBCATID")%>'