首页 新闻 搜索 专区 学院

热切请教:GridView调用PageIndexChanging绑定数据问题.

0
悬赏园豆:50 [已解决问题] 解决于 2010-11-02 19:51

谢谢各位高手,请教一个基础的问题.这是一个购物车页面后台代码.

在前页面,已经添加Session["shopcar"]成功.

在gridview列中绑定了"NO,,goodsID,_name"等等字段,调用PageIndexChanging事件.

但是,购物车GridView是空的,在页面上不显示.

望不吝赐教!!!

 

1 using System;
2  using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12 using System.Text.RegularExpressions;
13
14
15 public partial class cart : System.Web.UI.Page
16 {
17 DBClass sqloperate = new DBClass();
18 DataTable dtTable;
19 string sqlstr;
20 Hashtable ht;
21
22
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 if (!IsPostBack)
26 {
27 ST_check_Login();
28
29
30 if (Session["ShopCar"] == null)
31 {
32 ......提示信息
33
34 }
35 else
36 {
37
38 ht = (Hashtable)Session["ShopCart"];
39
40 if (ht.Count == 0)
41 {
42
43
44 .........提示信息
45 }
46 else
47 {
48
49 dtTable = new DataTable();
50
51 DataColumn column1 = new DataColumn("No"); //序号列
52 DataColumn column2 = new DataColumn("goodsID"); //商品ID代号
53 DataColumn column3 = new DataColumn("_name"); //商品名称
54 DataColumn column4 = new DataColumn("num"); //数量
55 DataColumn column5 = new DataColumn("price"); //单价
56 DataColumn column6 = new DataColumn("totalfee");//总价
57 dtTable.Columns.Add(column1); //添加新列
58 dtTable.Columns.Add(column2);
59 dtTable.Columns.Add(column3);
60 dtTable.Columns.Add(column4);
61 dtTable.Columns.Add(column5);
62 dtTable.Columns.Add(column6);
63 DataRow row;
64
65
66 foreach (object key in ht.Keys)
67 {
68 row = dtTable.NewRow();
69 row["goodsID"] = key.ToString;
70 row["num"] = ht[key].ToString();
71 dtTable.Rows.Add(row);
72 }
73
74 DataTable dstable;
75 int i = 1;
76 float price;
77 float totalfee = 0;
78 int count;
79
80 foreach (DataRow drRow in dtTable.Rows)
81 {
82 sqlstr = "select _name,fee from goods_info where goodsID=" + Convert.ToInt32(drRow["goodsID"].ToString()); ;
83 dstable = sqloperate.GetDataSetStr(sqlstr, "tbGI");
84 drRow["No"] = i;
85 drRow["_name"] = dstable.Rows[0][0].ToString();//商品名称
86 drRow["price"] = (dstable.Rows[0][1].ToString());//单价
87 price = float.Parse(dstable.Rows[0][1].ToString());//单价
88 count = Int32.Parse(drRow["Num"].ToString());
89 drRow["totalfee"] = price * count; //总价
90 totalfee += price * count; //计算合价
91 i++;
92
93 }
94 this.Label2.Text = "商品总计花费:" + totalfee.ToString();
95 this.GridView1.DataSource = dtTable.DefaultView;
96 this.GridView1.DataKeyNames = new string[] { "goodsID" };
97 this.GridView1.DataBind();
98 }
99 }
100 }
101
102
103
104 }
105 public void ST_check_Login()
106 {
107
108 if ((Session["username"] == null))
109 {
110 Response.Write("<script>alert('对不起!您不是会员,请先注册!');location='Default.aspx'</script>");
111 Response.End();
112 }
113 }
114
115
116 public void bind()
117 {
118
119
120 if (Session["ShopCar"] == null)
121 {...............提示信息
122
123 }
124 else
125 {
126
127 ht = (Hashtable)Session["ShopCart"];
128
129
130 if (ht.Count == 0)
131 {
132 ......提示信息
133 }
134 else
135 {
136
137 dtTable = new DataTable();
138
139 DataColumn column1 = new DataColumn("No"); //序号列
140 DataColumn column2 = new DataColumn("goodsID"); //商品ID代号
141 DataColumn column3 = new DataColumn("_name"); //商品名称
142 DataColumn column4 = new DataColumn("num"); //数量
143 DataColumn column5 = new DataColumn("price"); //单价
144 DataColumn column6 = new DataColumn("totalfee");//总价
145 dtTable.Columns.Add(column1); //添加新列
146 dtTable.Columns.Add(column2);
147 dtTable.Columns.Add(column3);
148 dtTable.Columns.Add(column4);
149 dtTable.Columns.Add(column5);
150 dtTable.Columns.Add(column6);
151 DataRow row;
152
153
154 foreach (object key in ht.Keys)
155 {
156 row = dtTable.NewRow();
157 row["goodsID"] = key.ToString();
158 row["num"] = ht[key].ToString();
159 dtTable.Rows.Add(row);
160
161 }
162
163 DataTable dstable;
164 int i = 1;
165 float price;
166 float totalfee = 0;
167 int count;
168
169 foreach (DataRow drRow in dtTable.Rows)
170 {
171 sqlstr = "select _name,fee from goods_info where goodsID=" + Convert.ToInt32(drRow["goodsID"].ToString()); ;
172 dstable = sqloperate.GetDataSetStr(sqlstr, "tbGI");
173 drRow["No"] = i;
174 drRow["_name"] = dstable.Rows[0][0].ToString();//商品名称
175 drRow["price"] = (dstable.Rows[0][1].ToString());//单价
176 price = float.Parse(dstable.Rows[0][1].ToString());//单价
177 count = Int32.Parse(drRow["Num"].ToString());
178 drRow["totalfee"] = price * count; //总价
179 totalfee += price * count; //计算合价
180 i++;
181
182 }
183 this.Label2.Text = "商品总计花费:" + totalfee.ToString();
184 this.GridView1.DataSource = dtTable.DefaultView;
185 this.GridView1.DataKeyNames = new string[] { "goodsID" };
186 this.GridView1.DataBind();
187 }
188
189
190 }
191
192 }
193 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
194 {
195 GridView1.PageIndex = e.NewPageIndex;
196 bind();
197
198 }
199
200 }
201
202
203
204
205
206

 

问题补充: 就算不分页,也不显示任何信息.不知道代码里面哪里有问题.
张洪玮的主页 张洪玮 | 菜鸟二级 | 园豆:224
提问于:2010-11-02 12:27
< >
分享
最佳答案
0

你返回的就是一个datatable,还要自己定义一个table把值填进去再绑定,这个我就不明白。

再者你绑定table感觉好麻烦,请将代码优化一下吧,bind()函数的内容也没必要写两次。

你的变量为什么在函数外定义?那里用到哪里定义好了

收获园豆:40
茂茂 | 老鸟四级 |园豆:2887 | 2010-11-02 14:15
说的很对,谢过.
张洪玮 | 园豆:224 (菜鸟二级) | 2010-11-02 19:50
其他回答(3)
0
like%'远远'% | 园豆:635 (小虾三级) | 2010-11-02 12:59
如果不分页,也没有任何显示
支持(0) 反对(0) 张洪玮 | 园豆:224 (菜鸟二级) | 2010-11-02 13:07
0

先确认 你分页事件有没有效果, 有没有调用, 调用了的话, 再看 里面调用的方法 有没有起作用: bind()有没有加载对应页的数据。

收获园豆:10
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-11-02 15:48
0

Hashtable ht = new Hashtable();
            ht.Add("1", "one");
            ht.Add("2", "two");
            DataTable dtTable = new DataTable();
            DataColumn column1 = new DataColumn("No");        //序号列 52 
            DataColumn column2 = new DataColumn("goodsID");    //商品ID代号 53   
            DataColumn column3 = new DataColumn("_name");  //商品名称 54  
            DataColumn column4 = new DataColumn("num");       //数量 55    
            DataColumn column5 = new DataColumn("price");     //单价 56  
            DataColumn column6 = new DataColumn("totalfee");//总价 57  
            dtTable.Columns.Add(column1);  //添加新列              
            dtTable.Columns.Add(column2);
            dtTable.Columns.Add(column3);
            dtTable.Columns.Add(column4);
            dtTable.Columns.Add(column5);
            dtTable.Columns.Add(column6);
            DataRow row;
            foreach (object key in ht.Keys)
            {
                row = dtTable.NewRow();
                row["goodsID"] = key.ToString();
                row["num"] = ht[key].ToString();
                dtTable.Rows.Add(row);

            }
            this.GridView1.DataSource = dtTable.DefaultView;
            this.GridView1.DataKeyNames = new string[] { "goodsID" };
            this.GridView1.DataBind();

 

 

至少这段没有什么问题,LZ你看下GridView配置。

萧鼎 | 园豆:106 (初学一级) | 2010-11-02 16:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册