问题描述
第一个页面的主要代码:<tablealign="center"><tr><tdstyle="width:502px;height:190px"><asp:GridViewID="GridView1"runat="server"BackColor="White"BorderColor="#CC9966"BorderStyle="None"BorderWidth="1px"CellPadding="4"AllowPaging="True"AutoGenerateColumns="False"PageSize="5"Width="100%"OnPageIndexChanging="GridView1_PageIndexChanging"><FooterStyleBackColor="#99CC00"ForeColor="#330099"/><RowStyleBackColor="White"ForeColor="#330099"/><SelectedRowStyleBackColor="#FFCC66"Font-Bold="True"ForeColor="#663399"/><PagerStyleBackColor="#FFFFCC"ForeColor="#330099"HorizontalAlign="Center"/><HeaderStyleBackColor="#990000"Font-Bold="True"ForeColor="#FFFFCC"/><Columns><asp:TemplateField><ItemTemplate><divstyle="text-align:left"><tableborder="0"cellpadding="0"cellspacing="0"style="width:483px;height:86px"align="center"><tralign="left"><tdrowspan="4"align="left"><asp:ImageID="Image1"Width="100px"Height="120"runat="server"ImageUrl='<%#"~/picture/"+Eval("bookImage")%>'/></td><tdalign="left">书名:<asp:Labelrunat="server"ID="Label1"Text='<%#Eval("bookName")%>'></asp:Label></td></tr><tralign="left"><tdalign="left">作者:<asp:Labelrunat="server"ID="Label2"Text='<%#Eval("bookAuthor")%>'></asp:Label></td></tr><tralign="left"><tdalign="left">价格:<asp:Labelrunat="server"ID="Label3"Text='<%#Eval("bookPrice")%>'></asp:Label></td></tr><tralign="left"><tdalign="left"><asp:ButtonID="Button1"runat="server"Text="加进购物车"CommandName="add"CommandArgument='<%#Eval("bookID")%>'/></td></tr></table></div></ItemTemplate></asp:TemplateField></Columns></asp:GridView></td></tr><tr><td><ahref="Default4.aspx">查看购物车</a></td></tr></table>第一个页面的.cs代码:publicpartialclassDefault3:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){DLBind();}}publicvoidDLBind(){//绑定商品展示台信息SqlConnectioncon=newSqlConnection(ConfigurationManager.AppSettings["conStr"]);con.Open();stringsqlStr="select*fromtblBook";SqlDataAdapterda=newSqlDataAdapter(sqlStr,con);DataSetds=newDataSet();da.Fill(ds,"tblBook");GridView1.DataSource=ds.Tables["tblBook"].DefaultView;GridView1.DataBind();da.Dispose();ds.Dispose();con.Close();}privateorderItemInfogetOdrerItem(objectsender,GridViewCommandEventArgse){orderItemInfoitem=neworderItemInfo();Buttonbt=e.CommandSourceasButton;//被单击的按钮GridViewRowrow=bt.Parent.ParentasGridViewRow;//被选定的行item.bookID=int.Parse(bt.CommandArgument);//书号Labelbook=(Label)row.FindControl("label1");//书名item.bookName=book.Text;item.bookNum=1;//数量Labelp=(Label)row.FindControl("label3");//价格item.price=Convert.ToDecimal(p.Text.Trim());returnitem;}protectedvoidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){if(Session["userlogin"]!=null){//添加购物车if(e.CommandName.ToLower()=="add"){orderinfoorder=null;//存储购物车信息orderItemInfoorderItem=null;//购买书籍的信息//判断购物车中是否有书籍if(Session["order"]==null){orderItem=getOdrerItem(sender,e);//获取购买的书籍if(orderItem==null){Response.Write("<scriptlanguage='javascript'>alert('您还没有购买任何书籍!');</script>");return;}//创建购物车order=neworderinfo();order.orderItemList.Add(orderItem);//添加新书到列表order.orderTime=DateTime.Now;//购买时间order.sumPrice=orderItem.price;//总价格order.TotalNum=orderItem.bookNum;//总数量Session["order"]=order;//记录订单}else//购物车中有书籍{order=(orderinfo)Session["order"];//将Session存入到orderorderItem=getOdrerItem(sender,e);inti=0;//判断购物车中是否已经有了此书for(i=0;i<order.orderItemList.Count;i++){//如果有此书,在此基础上加if(orderItem.bookID==((orderItemInfo)order.orderItemList[i]).bookID){((orderItemInfo)order.orderItemList[i]).bookNum++;//书籍数量加1((orderItemInfo)order.orderItemList[i]).price+=orderItem.price;break;}}if(i==order.orderItemList.Count)//购物车中没有此书,就将此书加到购物车{order.orderItemList.Add(orderItem);}order.orderTime=DateTime.Now;//购买时间order.TotalNum++;//总数量加1order.sumPrice+=orderItem.price;//总价格加1Session["order"]=order;}Response.Write("<scriptlanguage='javascritp'>alert('添加成功!');</script>");}}}protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse){GridView1.PageIndex=e.NewPageIndex;DLBind();}}下面还有`````
解决方案
解决方案二:
从上····点查看购物车的主要代码:<tablealign="center"style="width:600px"><tr><tdstyle="width:600px"><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"BackColor="White"BorderColor="#CC9966"BorderStyle="None"BorderWidth="1px"CellPadding="4"Width="100%"><FooterStyleBackColor="#FFFFCC"ForeColor="#330099"/><RowStyleBackColor="White"ForeColor="#330099"/><SelectedRowStyleBackColor="#FFCC66"Font-Bold="True"ForeColor="#663399"/><PagerStyleBackColor="#FFFFCC"ForeColor="#330099"HorizontalAlign="Center"/><HeaderStyleBackColor="#990000"Font-Bold="True"ForeColor="#FFFFCC"/><Columns><asp:TemplateFieldHeaderText="编号"><ItemTemplate><asp:LabelID="lblBookID"runat="server"Text="<%#((orderItemInfo)Container.DataItem).bookID%>"></asp:Label></ItemTemplate><ItemStyleWidth="40px"/></asp:TemplateField><asp:TemplateFieldHeaderText="书籍名称"><ItemStyleWidth="250px"/><ItemTemplate><asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl='<%#"bookinfo.aspx?bid="+((orderItemInfo)Container.DataItem).bookID%>'Text="<%#((orderItemInfo)Container.DataItem).bookName%>"></asp:HyperLink></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="价格"><ItemStyleWidth="80px"/><ItemTemplate><asp:LabelID="bookprice"runat="server"Text="<%#((orderItemInfo)Container.DataItem).price%>"></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="数量"><ItemStyleWidth="80px"/><ItemTemplate><asp:LabelID="booksum"runat="server"Text="<%#((orderItemInfo)Container.DataItem).bookNum%>"></asp:Label></ItemTemplate></asp:TemplateField></Columns></asp:GridView></td></tr><tr><tdstyle="height:27px"><asp:ButtonID="Button1"runat="server"Text="继续购买"OnClick="Button1_Click"/></td></tr></table>购物车.cs代码:publicpartialclassDefault4:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack)showShop();}publicvoidshowShop(){orderinfoorder=neworderinfo();order=(orderinfo)Session["order"];GridView1.DataSource=order.orderItemList;GridView1.DataBind();}protectedvoidButton1_Click(objectsender,EventArgse){Response.Redirect("Default3.aspx");}}错误:先浏览第一个页面,点击“加进购物车”然后在点点击“查看购物车“此时提示错误:GridView1.DataSource=order.orderItemList;(没有将对象引用设置为对象实例)order的值为空!要怎么改呢?我试过修改过,但还是没有成功!望各位大侠帮忙解决~!~
解决方案三:
以上是我的问题;有点长,希望各位有点耐心帮忙~····
解决方案四:
研究下,哎!!
解决方案五:
怎么都没得人来咯,各位大侠帮下忙呀~~~~~~~~~~