母版页不适用于ASP.NET页面

时间:2017-03-01 17:23:49

标签: asp.net

我的ASP.NET Webform遇到了问题。我的所有页面都与母版页完美配合,但由于某种原因,我的ProductItem页面不接受母版页。 继承我的ProductItem代码:

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ProductItem.aspx.cs" Inherits="mumsBoutique.ProductItem" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <link href="Content/websiteStyle.css" rel="stylesheet" type="text/css" />
    <h1><center>Product Details</center></h1>
<div class="row">
                        <asp:Label ID="productNameLabel" runat="server" Text="Product"></asp:Label>
                        <br />
                        <asp:Label ID="productItemNumber" runat="server" Text="ProductCode" CssClass="ProductItem_productNumber"></asp:Label>
                    </h3>


                    <h3>
                        <asp:Label ID="productCost" runat="server" Text="£0.00"></asp:Label>
                    </h3>
                <p>
                    <asp:Image ID="productImage" runat="server" />
                </p>

                <h4>Product summary</h4>
                <p>
                    <asp:Label ID="productDescription" runat="server" Text=""></asp:Label>
                </p>


        <!-- The right colum displays a text box for user to add items to the basket -->

            <h3>Buy Item</h3>
            <p>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="text-danger" />
                        <p>
                Quantity: <asp:TextBox ID="itemQuantity" runat="server" Text="1"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvItemQuantity" runat="server" ErrorMessage="Quantity is required" Text="Please specify the quantity required" ControlToValidate="itemQuantity" CssClass="text-danger" Display="Dynamic"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="cvItemQuantity" runat="server" ErrorMessage="Invalid quantity value" Text="Please specify a valid numeric quantity value" ControlToValidate="itemQuantity" Type="Integer" Display="Dynamic" CssClass="text-danger" Operator="DataTypeCheck"></asp:CompareValidator>
                <asp:RangeValidator ID="rvItemQuantity" runat="server" ErrorMessage="Invalid quantity value" Text="Please specify a quantity value between 1 and 10" ControlToValidate="itemQuantity" Type="Integer" MinimumValue="1" MaximumValue="10" Display="Dynamic" CssClass="text-danger"></asp:RangeValidator>
            </p>
           <p>
                <asp:Button ID="btnAddToBasket" runat="server" Text="Add to basket" CssClass="btn btn-primary btn-lg" OnClick="btnAddToBasket_Click" />
            </p>
</div>
</asp:Content>

但我不认为问题出现在这个特定页面上。当我从Bags.aspx页面上的超链接运行页面时,似乎会出现问题

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Bags.aspx.cs" Inherits="mumsBoutique.Bags" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <link href="Content/mumsBoutique.css" rel="stylesheet" type="text/css" />
    <h1><center>Bags</center></h1>
<div class="row">
        <asp:DataList ID="DataList1" runat="server" CssClass="table ProductsDataList" DataKeyField="productID" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatDirection="Horizontal">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Height="200px" Width="180px" ImageUrl='<%# Eval("Picture", "~/{0}") %>' />
                <br />
                <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl='<%# Eval("productID", "ProductItem.aspx/{0}") %>' Text='<%# Eval ("productName") %>'></asp:HyperLink>
                <br />
                Item Number:
                <asp:Label ID="ItemNumberLabel" runat="server" Text='<%# Eval("productNumber") %>' />
                <br />
                <asp:Label ID="CostLabel" runat="server" Text='<%# Eval("cost", "{0:C}") %>' />

                <br />
                <br />
<br />
            </ItemTemplate>
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT Items.productID, Items.productName, Items.productNumber, Items.picture, Items.cost, Items.categoryID, ItemCategories.Name AS Expr1 FROM ItemCategories INNER JOIN Items ON ItemCategories.categoryID = Items.categoryID WHERE (Items.categoryID = 2)"></asp:SqlDataSource>
    </div>

</asp:Content>

非常感谢任何帮助,被困了几个小时!

更新:以下是我的意思问题的一些屏幕截图。 This is the Bags.aspx Page当您点击超链接时,它会转到以下内容 ProductItem.aspx可以看出,主页面由于某种原因未被使用。然而在所有其他页面中,它的工作完全正常。

这是ProductItem.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.FriendlyUrls;
using mumsBoutique.Models;

namespace mumsBoutique
{
    public partial class ProductItem : System.Web.UI.Page
    {
            int _productID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            IList<string> segments = Request.GetFriendlyUrlSegments();

            if (segments != null && segments.Count > 0)
            {
                // Convert the item ID to an integer and store it in the field _itemID
                int.TryParse(segments[0], out _productID);
            }

            // If no item ID is specified, go back to the products page
            if (_productID == 0)
            {
                Response.Redirect("Products");
            }

            // If _itemID is a valid value, search the database for the matching item
            if (!IsPostBack && _productID > 0)
            {
                // open the connection to the database
                using (ApplicationDbContext context = new ApplicationDbContext())
                {


                    string sql = "SELECT * FROM Items WHERE productId = @productId";
                    System.Data.SqlClient.SqlParameter parameter = new System.Data.SqlClient.SqlParameter("@productId", _productID);
                    Item foundItem = context.Database.SqlQuery<Item>(sql, parameter).FirstOrDefault();

                    productNameLabel.Text = foundItem.productName;
                    productItemNumber.Text = foundItem.productNumber;
                    productCost.Text = foundItem.cost.ToString("c");
                    productImage.ImageUrl = "~/" + foundItem.picture;
                    productDescription.Text = foundItem.productDecription;
                }
            }
        }

        protected void btnAddToBasket_Click(object sender, EventArgs e)
        {
            // Check if page is valid
            if (Page.IsValid)
            {
                int qty = 0;

                // Get the quantity from the input field
                qty = int.Parse(itemQuantity.Text);

                // Check if item already exist in basket
                OrderItem basketItem = ShopApp.Instance.GetBasketItem(_productID);

                if (basketItem == null)
                {
                    // item does not currently exist in basket, add new one
                    using (ApplicationDbContext context = new ApplicationDbContext())
                    {

                        // find the product item (pItem) with ItemId matching what is stored in the variable field (_itemId)
                        Item productItem = context.Items.FirstOrDefault(i => i.productID == _productID);
                        basketItem = new OrderItem()
                        {
                            productId = _productID,
                            Quantity = qty,
                            Price = double.Parse(productCost.Text, System.Globalization.NumberStyles.Currency),
                            Item = productItem
                        };
                        ShopApp.Instance.BasketItems.Add(basketItem);
                    }
                }
                else
                {
                    // item already exists in basket, increase the quantity
                    basketItem.Quantity += qty;
                }


                ((SiteMaster)Master).UpdateBasket();

            }

        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您似乎尝试将productID作为参数传递,因此请尝试更改NavigateUrl='<%# Eval("productID", "ProductItem.aspx/{0}") %>'

NavigateUrl='<%# Eval("productID", "ProductItem.aspx?productID={0}") %>'

此外,您需要将CSS链接移动到母版页中的适当位置 - 在使用母版页时,不应该从页面内链接到CSS样式表,当然也不要在ContentPlaceHolder部分中链接到CSS样式表没有压倒任何风格。这很可能是导致问题的原因。

<强>更新

我已经看过你的项目并发现了以下问题,只是通过观察它(由于diff DB版本无法运行它等):

  1. 主页中的标记格式错误。你基本上是在头部开展所有工作,因为你没有身体部分
  2. 母版页中样式表的链接位于 HTML文档 - 它应该在head部分内。以下是主页面结构的外观:

    <%@ Master Language="C#" AutoEventWireup="true"
        CodeBehind="Site.master.cs" Inherits="mumsBoutique.SiteMaster" %> 
    <!DOCTYPE html>  
    <html lang="en">
    <head runat="server">
        <meta charset="utf-8" />
        <link href="Content/websiteStyle.css" rel="stylesheet"
            type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
            ...
        </form>
    </body>
    

  3. 从Bags和ProductItem中删除多余的CSS链接 页面 - 当您修复母版页中的CSS链接时,即 所有你需要的;你显然没有覆盖任何东西,因为你在整个过程中都指定了相同的样式表。