大家好,这个是我在CSDN发的第一个帖子,应该也没有什么分给大家,等有分了再给大家,希望能得到大家的热心帮助!
我做了个小购物车的页面,做了个分页,遇见了一个很奇怪的问题,现在还没有解决,希望高手指点迷津.
GRIDVIEW分页时候,页面的数值也显示了,比如 1 2 3 4 5 6,第一页能够正常显示,当我点第2页时,竟然不显示第2爷的内容,而是显示购物车页面的内容,就像是触动了BUYBUTTON似的,物品名称竟然是PAGE,我晕.
于是,我就把购物的BUYBUTTON的功能给除掉试试,结果1 2 3 4页面都能显示,而点击5 6是竟然显示索引超出范围,必须为非负或范围大小内,错误名称是INDEX.
对于两个问题出在什么地方,怎么解决,希望能得到高手帮助.
另外还有个问题,就是自定义分页,要用到COMMANDARGUMENT,怎么才能不和GRIDVIEW中的COMMANDARGUMENT冲突.
我做了个小购物车的页面,做了个分页,遇见了一个很奇怪的问题,现在还没有解决,希望高手指点迷津.
GRIDVIEW分页时候,页面的数值也显示了,比如 1 2 3 4 5 6,第一页能够正常显示,当我点第2页时,竟然不显示第2爷的内容,而是显示购物车页面的内容,就像是触动了BUYBUTTON似的,物品名称竟然是PAGE,我晕.
于是,我就把购物的BUYBUTTON的功能给除掉试试,结果1 2 3 4页面都能显示,而点击5 6是竟然显示索引超出范围,必须为非负或范围大小内,错误名称是INDEX.
对于两个问题出在什么地方,怎么解决,希望能得到高手帮助.
另外还有个问题,就是自定义分页,要用到COMMANDARGUMENT,怎么才能不和GRIDVIEW中的COMMANDARGUMENT冲突.
------------------------------------------------------------------------------------------
先根据CommandName判断,再根据CommandArgumnet属性判断.
MSDN ex.
<%@ Page Language="C#" AutoEventWireup="True" %><html>
<head> <script runat="server"> void CommandBtn_Click(Object sender, CommandEventArgs e)
{ switch(e.CommandName)
{ case "Sort": // Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break; case "Submit": // Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button"; // Test whether the command argument is an empty string ("").
if((String)e.CommandArgument == "")
{
// End the message.
Message.Text += ".";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized.";
}
break; default: // The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized.";
break; } } void Sort_List(string commandArgument)
{ switch(commandArgument)
{ case "Ascending":
// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button.";
break; case "Descending":
// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button.";
break; default:
// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized.";
break; } } </script></head>
<body> <form runat="server"> <h3>Button CommandName Example</h3> Click on one of the command buttons. <br><br>
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/> <asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/> <br><br> <asp:Button id="Button3"
Text="Submit"
CommandName="Submit"
OnCommand="CommandBtn_Click"
runat="server"/> <asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/> <asp:Button id="Button5"
Text="Submit Unknown Command Argument"
CommandName="Submit"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<br><br> <asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
楼主赶紧把代码帖出来,大伙帮分析一下啊,这样啥内容都没有晾在上面想干么?
1.Buy.ascx及Buy.ascx.cs是TreeView目录,就是产品的目录,点击就传递CategoryID值.
2.ProductUC.ascx及ProductUC.ascx.cs就是接受到CategoryID的值来绑定Product.ascx中的GridView的ProductView.
3.Order.ascx及代码文件Order.ascx.cs就是2中购物按钮通过Session传值到Order.ascx中的GridView的OrderView,再进一部确定就添加到数据库.
我是将1和2放在Shopping.aspx中,点击2中的购物按钮就跳转到Order.aspx
其中1和3的程序没有问题,问题在2中.因此,我只将2的代码贴出来
ProductUC.ascx//显示具体商品的web控件
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductUC.ascx.cs" Inherits="ProductUC" %>
<asp:GridView ID="ProductView" runat="server" Width="100%" AutoGenerateColumns="False" OnPageIndexChanging="ProductView_PageIndexChanging" DataKeyNames="ProductID" ShowHeader="False" OnRowCommand="ProductView_RowCommand" OnRowCreated="ProductView_RowCreated" AllowPaging="true" PageSize="5" PagerStyle-HorizontalAlign="Center" PagerStyle-Font-Underline="true" AllowSorting="true">
<FooterStyle ForeColor="White" BackColor="#3B6BD1" Font-Bold="True"></FooterStyle>
<SelectedRowStyle Font-Bold="True" ForeColor="Navy" BackColor="#FFCC66" BorderColor="CornflowerBlue" />
<RowStyle ForeColor="#333333" BackColor="#ffffff" BorderColor="CornflowerBlue" BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<ul id="pv">
<ul id="pv1">
<a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank"><img src="Images/<%# DataBinder.Eval(Container.DataItem,"PicName") %>" /></a>
<li>商品名称:<span class="pv1-s1"><a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank"><%# DataBinder.Eval(Container.DataItem,"Name")%></a></span> 商品价格:<span id="pv1-s2"><asp:Label ID="Price" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"OutPrice")%>'></asp:Label></span>
</li>
<li>商品规格:<span class="pv1-s2"><%# DataBinder.Eval(Container.DataItem,"Sell")%></span> 商品材质:<span class="pv1-s1"><%# DataBinder.Eval(Container.DataItem,"CreateDate")%></span>
</li>
<li> 详细介绍:<span class="pv1-s1"><%# DataBinder.Eval(Container.DataItem,"Desn")%>
</span>
</li>
<li><hr/></li>
</ul>
<ul id="pv2">
<li>
<a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank">商品详细信息</a>
</li>
<li>
<a href='../Desktop/Comment.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank">查看商品评论</a>
</li>
<li>
<asp:Button ID="BuyBtn" Runat="server" CssClass="buttoncss" Text="加入购物车" Width="80" CommandName='<%#DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Button>
</li>
</ul>
</ul>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class ProductUC : System.Web.UI.UserControl
{
private int nCategoryID = -1;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["CategoryID"] != null)
{
if (Int32.TryParse(Request.Params["CategoryID"].ToString(), out nCategoryID) == false) { return; }
}
if (!Page.IsPostBack)
{
if (nCategoryID > 0)
{ BindProductData(nCategoryID); ///根据TreeView中的CategoryID绑定ProductView
}
else
{
BindProductData(2);///如果没有收到TreeView中的CategoryID值,则确定CategoryID=2
}
} }
private void BindProductData(int nCategoryID)///根据TreeView中的CategoryID绑定ProductView
{
Product product = new Product();
SqlDataAdapter da = product.GetProductByCategory(nCategoryID);///运用逻辑层中product.cs定义好的Product的GetProductByCategory()函数
DataSet ds = new DataSet();
da.Fill(ds);
ProductView.DataSource = ds;
ProductView.DataBind();
}
protected void ProductView_PageIndexChanging(object sender, GridViewPageEventArgs e)///分页时候绑定选定的页为主页面
{
ProductView.PageIndex = e.NewPageIndex;
BindProductData(nCategoryID);
}
protected void ProductView_RowCommand(object sender, GridViewCommandEventArgs e)
{
OrderItemInfo item = null; OrderInfo order = null;
if (Session[Session.SessionID + OrderForm.Cart] == null)
{
item = GetOrderItemInformation(e);
if (item == null)
{
Response.Write("<script>window.alert('数据错误')</script>");
return;
}
order = new OrderInfo();
order.OrderItemList.Add(item);
order.TotalMoney = item.Price;
order.TotalNumber = item.Number;
Session[Session.SessionID + OrderForm.Cart] = order;
}
else
{
order = (OrderInfo)Session[Session.SessionID + OrderForm.Cart];
item = GetOrderItemInformation(e);
if (item == null)
{
Response.Write("<script>window.alert('数据错误')</script>");
return;
}
int i = 0;
for (i = 0; i < order.OrderItemList.Count; i++)
{
if (item.ProductID == ((OrderItemInfo)order.OrderItemList[i]).ProductID)
{
((OrderItemInfo)order.OrderItemList[i]).Number++;
((OrderItemInfo)order.OrderItemList[i]).ItemTotalMoney += item.ItemTotalMoney;
break;
}
}
if (i == order.OrderItemList.Count) { order.OrderItemList.Add(item); }
order.TotalNumber++;
order.TotalMoney += item.Price;
Session[Session.SessionID + OrderForm.Cart] = order;
}
Response.Redirect("Order.aspx");
}
private OrderItemInfo GetOrderItemInformation(GridViewCommandEventArgs e)
{
///当购物车没有任何信息时
OrderItemInfo item = new OrderItemInfo();
item.Date = DateTime.Now;
item.Number = 1; ///获取商品价格
Label priceL = (Label)ProductView.Rows[
Int32.Parse(e.CommandArgument.ToString())].FindControl("Price");
if (priceL == null)
{
///显示错误信息
Response.Write("<script>window.alert('数据错误')</script>");
return null;
}
item.Price = item.ItemTotalMoney = decimal.Parse(priceL.Text.Trim());
///获取商品ID
item.ProductID = Int32.Parse(ProductView.DataKeys[
Int32.Parse(e.CommandArgument.ToString())].Value.ToString());
///获取商品名称
item.Name = e.CommandName;
return (item);
}
protected void ProductView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button buyBtn = (Button)e.Row.FindControl("BuyBtn");
if (buyBtn != null)
{
buyBtn.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
}
于是我把文中的红色代码删掉,及除去BuyButton的购买功能,于是点击 1 2 3 4都可以显示产品,但点击5或者6的时候,[color=#FF0000]显示的页面是索引超出范围。必须为非负值并小于集合大小。
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误:
行 105:
行 106: ///获取商品价格
行 107: Label priceL = (Label)ProductView.Rows[
行 108: Int32.Parse(e.CommandArgument.ToString())].FindControl("Price");
行 109: if (priceL == null)
源文件: e:\Example_4\ProductUC.ascx.cs 行: 107 [/color]
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProductUC.ascx.cs" Inherits="ProductUC" %>
<asp:GridView ID="ProductView" runat="server" Width="100%" AutoGenerateColumns="False" OnPageIndexChanging="ProductView_PageIndexChanging" DataKeyNames="ProductID" ShowHeader="False" OnRowCommand="ProductView_RowCommand" OnRowCreated="ProductView_RowCreated" AllowPaging="true" PageSize="5" PagerStyle-HorizontalAlign="Center" PagerStyle-Font-Underline="true" AllowSorting="true">
<FooterStyle ForeColor="White" BackColor="#3B6BD1" Font-Bold="True"></FooterStyle>
<SelectedRowStyle Font-Bold="True" ForeColor="Navy" BackColor="#FFCC66" BorderColor="CornflowerBlue" />
<RowStyle ForeColor="#333333" BackColor="#ffffff" BorderColor="CornflowerBlue" BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<ul id="pv">
<ul id="pv1">
<a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank"><img src="Images/<%# DataBinder.Eval(Container.DataItem,"PicName") %>" /></a>
<li>
商品名称:<span class="pv1-s1"><a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank"><%# DataBinder.Eval(Container.DataItem,"Name")%></a></span> 商品价格:<span id="pv1-s2"><asp:Label ID="Price" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"OutPrice")%>'></asp:Label></span>
</li>
<li>
商品规格:<span class="pv1-s2"><%# DataBinder.Eval(Container.DataItem,"Sell")%></span> 商品材质:<span class="pv1-s1"><%# DataBinder.Eval(Container.DataItem,"CreateDate")%></span>
</li>
<li>
详细介绍:<span class="pv1-s1"> <%# DataBinder.Eval(Container.DataItem,"Desn")%>
</span>
</li>
<li><hr/></li>
</ul>
<ul id="pv2">
<li>
<a href='../Admin/Product/ProductInfo.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank">商品详细信息</a>
</li>
<li>
<a href='../Desktop/Comment.aspx?ProductID=<%# DataBinder.Eval(Container.DataItem,"ProductID")%>' target="_blank">查看商品评论</a>
</li>
<li>
<asp:Button ID="BuyBtn" Runat="server" CssClass="buttoncss" Text="加入购物车" Width="80" CommandName='<%#DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Button>
</li>
</ul>
</ul>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
(舞指如歌)
等 级:
发表于:2008-01-22 14:33:4029楼 得分:0
建议使用存储过程分页,简单且效率高不会出错。
兄弟,我试试,结帖会给你50点