问题描述
我现在在做一个产品对比的功能(类似于淘宝的产品对比)啊用的是vs2005语言是C#我已经做了几天了,好累啊,希望大家帮帮我,前台部分代码:<asp:ButtonID="Button2"runat="server"Text="对比选择商品"OnClick="Button2_Click"/><br/><asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"CellPadding="4"ForeColor="#333333"GridLines="None"OnPageIndexChanging="GridView1_PageIndexChanging"Width="100%"AutoGenerateColumns="False"><FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/><Columns><asp:TemplateFieldFooterText="对比选择"HeaderText="对比选择"><ItemTemplate><inputid="Checkbox1"type="checkbox"value='<%#DataBinder.Eval(Container.DataItem,"GoodId")%>'/></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="图片"><EditItemTemplate> </EditItemTemplate><ItemTemplate><ahref='Sellgoodlist.aspx?categoryid=<%#DataBinder.Eval(Container.DataItem,"GoodId")%>'><asp:ImageID="Image1"runat="server"Height="150"ImageUrl='<%#Eval("GoodPicture")%>'Width="150"/></a></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="标题"><ItemTemplate>:<asp:LabelID="Label1"runat="server"Text='<%#Eval("InfoTitle")%>'></asp:Label><br/>用户id<spanstyle="color:#0000ff;text-decoration:underline">:</span><asp:LabelID="Label3"runat="server"Text='<%#Eval("UserName")%>'></asp:Label><br/>信用:<spantitle="一钻卖家(101-300个信用积分)"><imgalign="middle"src="image/seller01.gif"/><br/><div>QQ:<imgalt="我在啊!!"height="22"src="image/qqonline.gif"width="16"/><ahref="#"onclick="javascript:AddFavoriteItemEx2('',this,[44,0]);"title="收藏此商品">收藏</a></div></span></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="新旧"><ItemTemplate> <asp:LabelID="Label5"runat="server"Text='<%#Bind("NewOld")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="数量"><ItemTemplate><asp:LabelID="Label6"runat="server"Text='<%#Bind("GoodQuantity")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="价格"><ItemTemplate><asp:LabelID="Label2"runat="server"Text='<%#Bind("GoodPrice")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="所在地"><EditItemTemplate><asp:TextBoxID="TextBox5"runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate> <asp:LabelID="Label4"runat="server"Text='<%#Eval("GoodAddress")%>'></asp:Label></ItemTemplate></asp:TemplateField></Columns><RowStyleBackColor="#EFF3FB"/><EditRowStyleBackColor="#2461BF"/><SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/><PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/><HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/><AlternatingRowStyleBackColor="White"/></asp:GridView><asp:ButtonID="Button6"runat="server"Text="对比选择商品"/>后台:publicpartialclassSellgood:System.Web.UI.Page{stringsqlstr;SqlConnectioncon;protectedvoidPage_Load(objectsender,EventArgse){sqlstr=ConfigurationManager.AppSettings["ConnectionString"];con=newSqlConnection(sqlstr);con.Open();Stringcmd="SELECTGoodId,GoodPicture,InfoTitle,UserName,NewOld,GoodQuantity,GoodPrice,GoodAddressFROMSellGood";SqlDataAdaptermyda=newSqlDataAdapter(cmd,con);DataSetmyds=newDataSet();myda.Fill(myds);this.GridView1.DataSource=myds;this.GridView1.DataBind();this.GridView1.AllowPaging=true;this.GridView1.PageSize=10;con.Close();}protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse){this.GridView1.PageIndex=e.NewPageIndex;this.GridView1.DataBind();}protectedvoidbtnSearch_Click(objectsender,EventArgse){stringkey=this.txtSKWord.Text.Replace(""," ").ToString();Response.Redirect("Search.aspx?key="+Server.UrlEncode(key));}protectedvoidButton2_Click(objectsender,EventArgse){sqlstr=ConfigurationManager.AppSettings["ConnectionString"];con=newSqlConnection(sqlstr);con.Open();for(inti=0;i<=GridView1.Rows.Count-1;i++){CheckBoxcbox=(CheckBox)GridView1.Rows[i].FindControl("CheckBox1");if(cbox.Checked==true){stringsql="selectGoodId,GoodPicture,InfoTitle,UserName,NewOld,GoodQuantity,GoodPrice,GoodAddressFROMSellGoodwhereGoodId='"+GridView1.DataKeys[i].Value+"'";SqlCommandsqlcom=newSqlCommand(sql,con);con.Open();sqlcom.ExecuteNonQuery();con.Close();}}stringsqlstr="select*fromGoodSell";SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,con);DataSetmyds=newDataSet();myda.Fill(myds,"SellGood");GridView1.DataSource=myds;GridView1.DataKeyNames=newstring[]{"GoodId"};GridView1.DataBind();con.Close();}只能显示选中的最后一行数据啊,我想显示选中的所有数据啊
解决方案
解决方案二:
我不知道你具体要做什么,如果是产品对比的话,其时是比较简单就可以实现的你可以定义一个Session然后,初始化一个List放到Session中。当用户每次选择一个产品,即将产品的ID放入List,然后在将List存入Session在出较产品的时候,将List中产品ID,依次从数据库中读出即可