在asp.net中运行时从db创建动态页面

时间:2016-06-22 04:51:16

标签: asp.net vb.net global-asax dynamic-pages

我正在制作电子商务网站。在此之下,我想展示特定的产品细节。从查询字符串我可以这样做,但这不是seo友好所以我需要在url(从db)而不是使用查询字符串中创建产品名称。我尝试了以下代码,但它对我不起作用。

错误 - 无法找到资源

Global.asax中

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Optimization" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    Sub Application_Start(sender As Object, e As EventArgs)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
        BundleConfig.RegisterBundles(BundleTable.Bundles)
    End Sub

    Private Shared Sub RegisterRoutes(routes As RouteCollection)
        routes.MapPageRoute("product-detail", "{product_name}.aspx", "product-detail.aspx")
    End Sub

</script>

product-detail.aspx(动态页面)

Private Sub product_detail_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim pageName As String = Me.Page.RouteData.Values("product_name").ToString()
        End If
    End Sub

shop.aspx(使用listview控件显示产品列表)

<asp:ListView ID="products" runat="server" DataKeyNames="ID">
                            <ItemTemplate>
                                <asp:HyperLink ID="productID" runat="server" NavigateUrl='<%# Eval("product_name", "~/{0}") %>' CssClass="product-link">
                            <!--blocks-starts--><div class="blocks blocks-shop">
                                <asp:Image ID="readyStock" runat="server" ImageUrl="images/common/ready_stock_tag.png" Visible="false" CssClass="tag" />
                                <asp:Label ID="checkReadyStock" runat="server" Visible="false"></asp:Label>
                            <div class="block-img">
                                <img src='<%# Eval("image") %>' runat="server" id="proImg" />
                            </div>

                            <div class="block-content">
                                <span class="sku" style="font-size:0.6em !important">Item No. <asp:Label ID="skuID" runat="server" Text='<%# Eval("sku") %>'></asp:Label></span>
                                <h3>
                                    <asp:Label ID="prodName" runat="server" Text='<%# Eval("product_name") %>'></asp:Label></h3>
                                <p><strong>
                                    <asp:Label ID="priceRange" runat="server" Text='<%# Eval("price_range") %>'></asp:Label></strong></p>
                            </div>
                       </div><!--blocks-ends-->
                                </asp:HyperLink>
                            </ItemTemplate>

                            <LayoutTemplate>
                                <div id="itemPlaceholderContainer" runat="server" style="">
                                    <div runat="server" id="itemPlaceholder" />
                                </div>
                            </LayoutTemplate>
                        </asp:ListView>

shop.aspx.vb

Private Sub shop_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Try
                Dim str As String = "select * from products where status = 'active'"
                Dim cmd As New MySqlCommand(str, con)
                con.Open()
                Dim da As New MySqlDataAdapter(cmd)
                Dim dt As New DataTable
                da.Fill(dt)
                products.DataSource = dt
                products.DataBind()
                con.Close()
            Catch ex As Exception
                Response.Write(ex)
            End Try
        End If
    End Sub

0 个答案:

没有答案