【发布时间】:2017-03-01 17:23:48
【问题描述】:
我的 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();
}
}
}
}
【问题讨论】:
-
你的错误信息是什么?
-
没有错误消息,页面工作它只是没有使用母版页。
-
您检查母版页和页面路径了吗?由于母版页路径中的“~”字符,您可能会遇到问题。那么母版页在哪里?如果它在文件夹中,您可以尝试使用“..\Site.Master”
-
母版页不在任何特定文件夹中,所有页面都在项目名称下分组在一起。如果你看我上面的原始代码描述,它最初是没有“~”的。我尝试将其作为提到的答案之一,这可能是问题所在,但仍处于相同状态。我试过“..\Site.Master”,我得到这个错误:严重性代码描述项目文件行抑制状态错误CS1009无法识别的转义序列。我不确定我的错误是什么,但我怀疑它与路径有关。感谢您的回复,我愿意尝试任何事情。
-
Okey 那么你可以试试“Site.Master”而不是在路径中使用“~/”字符。
标签: asp.net