我们在做复式投票的时候。会遇到标题和选项嵌套的样子,下面我把我的代码放上来。
1,投票选择前台页面
# <table height="286" cellSpacing="0" cellPadding="0" width="1000" align="center" bgColor="#ffffff"
#
# border="0">
#
# <tbody>
#
# <tr>
#
# <td vAlign="top" height="286">
#
# <div align="left">
<table height="32" cellSpacing="0" cellPadding="0" width="968" align="center" border="0">
<tbody>
<tr>
<td>
<div align="center"></div>
#
# <div align="center"><strong><%=Title%></strong><br>
#
# </div>
#
# </td>
#
# </tr>
#
# </tbody>
#
# </table>
#
# <table cellSpacing="0" cellPadding="0" width="915" align="center" border="0">
#
# <tbody>
#
# <tr>
#
# <td background="http://www.cnblogs.com/images/house4_07.jpg" height="1"><FONT face="宋体"></FONT></td>
#
# </tr>
#
# <tr>
#
# <td>
#
# <table cellSpacing="0" cellPadding="0" width="915" border="0">
#
# <asp:repeater id="Repeater1" Runat="server">
#
# <ItemTemplate>
#
# <tr>
#
# <td>
#

# <%
# DataBinder.Eval(Container, "DataItem.FVoteName") %>
#
# <table width="85%" align="center" border="1" bordercolor="#D6E7FF" cellpadding="0" cellspacing="0">
#
# <tr>
#
# <td>
#
# <asp:CheckBoxList id="cb" Runat="server" Visible="False"></asp:CheckBoxList>
#
# <asp:RadioButtonList ID="rb" Runat="server" Visible="False"></asp:RadioButtonList>
#
# <asp:TextBox ID="tb" TextMode="MultiLine" Columns="40" Rows="4" Runat="server" Visible="False"></asp:TextBox>
#
# <input type="hidden" id="hb" runat="server" />
#
# </td>
#
# </tr>
#
# </table>
#
# </td>
#
# </tr>
#
# </ItemTemplate>
#
# </asp:repeater></table>
#
# </td>
#
# </tr>
#
# <tr>
#
# <td align="center"><asp:button id="btnAdd" runat="server" Text="提交"></asp:button></td>
#
# </tr>
#
# </tbody>
#
# </table>
#
# </div>
#
# <div align="right"></div>
#
# </td>
#
# </tr>
#
# </tbody>
#
# </table>
2,后台代码
1. <pre class="csharp" name="code">protected System.Web.UI.WebControls.Repeater Repeater1;
2.
3.
4.
5. public Seaskyer.Modules.Utils.DBClass db = new Seaskyer.Modules.Utils.DBClass();
6.
7. protected System.Web.UI.WebControls.Button btnAdd;
8.
9.
10.
11. public string Title;
12.
13. //public string cid;
14.
15.
16.
17. private void Page_Load(object sender, System.EventArgs e)
18.

19.
{
20.
21. if(!Page.IsPostBack)
22.

23.
{
24.
25. string cid = "";
26.
27. if(db.q("id")!="")
28.

29.
{
30.
31. cid=db.q("id");
32.
33. }
34.
35.
36.
37.
38.
39. Title = db.getSingleValue("cms_VotePro","FProName","FID",cid);
40.
41.
42.
43. BindRep();
44.
45. }
46.
47. }
48.
49.
50.

51. /**//// <summary>
52.

53. /**//// 绑定Repeater控件,显示调查中的大类
54.

55. /**//// </summary>
56.
57. public void BindRep()
58.

59.
{
60.
61. string strsql = "select * from cms_vote where FProID='"+db.q("id")+"'";
62.
63. db.dp.CommandText = strsql;
64.
65. DataTable dt = db.dp.DataTableSQL();
66.
67. if(dt.Rows.Count>0)
68.

69.
{
70.
71. this.Repeater1.DataSource = dt;
72.
73. this.Repeater1.DataBind();
74.
75. }
76.
77. }
78.
79.
80.
81.
82.
83.
84.
85.
86.

87. Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
88.
89. override protected void OnInit(EventArgs e)
90.

91.
{
92.
93. //
94.
95. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
96.
97. //
98.
99. InitializeComponent();
100.
101. base.OnInit(e);
102.
103. }
104.
105.
106.

107. /**//// <summary>
108.

109. /**//// 设计器支持所需的方法 - 不要使用代码编辑器修改
110.

111. /**//// 此方法的内容。
112.

113. /**//// </summary>
114.
115. private void InitializeComponent()
116.

117.
{
118.
119. this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
120.
121. this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
122.
123. this.Load += new System.EventHandler(this.Page_Load);
124.
125.
126.
127. }
128.
129. #endregion
130.
131.
132.
133. private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
134.

135.
{
136.
137. CheckBoxList cb = (CheckBoxList)e.Item.FindControl("cb");
138.
139. RadioButtonList rb = (RadioButtonList)e.Item.FindControl("rb");
140.
141. TextBox tb = (TextBox)e.Item.FindControl("tb");
142.
143. System.Web.UI.HtmlControls.HtmlInputHidden hb = (HtmlInputHidden)e.Item.FindControl("hb");
144.
145. DataRowView rowv = (DataRowView)e.Item.DataItem;
146.
147. //提取分类ID
148.
149. string Fid = rowv["FVoteId"].ToString();
150.
151. string type = rowv["FType"].ToString();
152.
153.
154.
155. string strsql = "select * from cms_VoteItem where FVoteId='"+Fid+"'";
156.
157.
158.
159. //Response.Write("<script>alert('"+strsql+"');</script>");
160.
161. db.dp.CommandText = strsql;
162.
163. DataTable dt = db.dp.DataTableSQL();
164.
165.
166.
167. switch(Convert.ToInt32(type))
168.

169.
{
170.
171. case 1:
172.
173. rb.Visible = true;
174.
175. cb.Visible = false;
176.
177. tb.Visible = false;
178.
179. rb.DataSource = dt;
180.
181. rb.DataTextField = "FItemName";
182.
183. rb.DataValueField = "FID";
184.
185. rb.DataBind();
186.
187. break;
188.
189. case 2:
190.
191. rb.Visible = false;
192.
193. cb.Visible = true;
194.
195. tb.Visible = false;
196.
197. cb.DataSource = dt;
198.
199. cb.DataTextField = "FItemName";
200.
201. cb.DataValueField = "FID";
202.
203. cb.DataBind();
204.
205. break;
206.
207. case 3:
208.
209. tb.Visible = true;
210.
211.
212.
213. rb.Visible = false;
214.
215. cb.Visible = false;
216.
217. break;
218.
219. }
220.
221. }
222.
223.
224.
225. private void btnAdd_Click(object sender, System.EventArgs e)
226.

227.
{
228.
229. string cb = "";//复选框
230.
231. string rb = "";//单选框
232.
233. string tb = "";//如果有文本框
234.
235. foreach (RepeaterItem item in this.Repeater1.Items)
236.

237.
{
238.
239.
240.
241. CheckBoxList cb1 = (CheckBoxList)item.FindControl("cb");
242.
243. RadioButtonList rb1 = (RadioButtonList)item.FindControl("rb");
244.
245. TextBox tb1 = (TextBox)item.FindControl("tb");
246.
247.
248.
249. //CheckBox cb = (CheckBox)item.FindControl("CheckBoxRole");
250.
251. if(cb1.Visible==true)
252.

253.
{
254.
255. for(int i=0;i<cb1.Items.Count;i++)
256.

257.
{
258.
259. if(cb1.Items[i].Selected==true)
260.

261.
{
262.
263. cb += "'"+cb1.Items[i].Value+"'" + ",";
264.
265. }
266.
267. }
268.
269. }
270.
271.
272.
273. if(rb1.Visible==true)
274.

275.
{
276.
277. for(int i=0;i<rb1.Items.Count;i++)
278.

279.
{
280.
281. if(rb1.Items[i].Selected==true)
282.

283.
{
284.
285. rb = "'"+rb1.Items[i].Value+"'";
286.
287. }
288.
289. }
290.
291. }
292.
293.
294.
295. if(tb1.Visible==true)
296.

297.
{
298.
299. tb = tb1.Text + "|";
300.
301. }
302.
303. }
304.
305.
306.
307. string id = "";
308.
309.
310.
311. if(cb.Trim()!="")
312.

313.
{
314.
315. cb = cb.Substring(0,cb.Length-1);
316.
317. id = cb;
318.
319. }
320.
321.
322.
323. if(rb.Trim()!="")
324.

325.
{
326.
327. if(id.Trim()!="")
328.

329.
{
330.
331. id += "," + rb;
332.
333. }
334.
335. else
336.

337.
{
338.
339. id += rb;
340.
341. }
342.
343. }
344.
345. //id = cb + rb;
346.
347.
348.
349. if(id.Trim()=="")
350.

351.
{
352.
353. Response.Write("<script>alert('你没有进行选择!');history.go(-1);</script>");
354.
355. }
356.
357. string strsql = "update cms_VoteItem set FItemNum=FItemNum+1 where FID in ("+id+")";
358.
359. db.dp.CommandText = strsql;
360.
361. db.dp.ExecuteNonQuery();
362.
363. Response.Write("<script>alert('投票结束!');location.href='VoteView.aspx?id="+db.q("id")+"';</script>");
364.
365. // Response.Write(id);
366.
367. }</pre>
3,显示投票结果前台
1. <table height="286" cellSpacing="0" cellPadding="0" width="1000" align="center" bgColor="#ffffff"
2.
3. border="0">
4.
5. <tbody>
6.
7. <tr>
8.
9. <td vAlign="top" height="286">
10.
11. <div align="left">
12.
13. <table height="32" cellSpacing="0" cellPadding="0" width="968" align="center" border="0">
14.
15. <tbody>
16.
17. <tr>
18.
19. <td>
20.
21. <div align="center"></div>
22.
23. <div align="center"><strong><%=Title%></strong><br>
24.
25. </div>
26.
27. </td>
28.
29. </tr>
30.
31. </tbody>
32.
33. </table>
34.
35. <table cellSpacing="0" cellPadding="0" width="915" align="center" border="0">
36.
37. <tbody>
38.
39. <tr>
40.
41. <td background="http://www.cnblogs.com/images/house4_07.jpg" height="1"><FONT face="宋体"></FONT></td>
42.
43. </tr>
44.
45. <tr>
46.
47. <td>
48.
49. <table cellSpacing="0" cellPadding="0" width="915" border="0">
50.
51. <asp:repeater id="Repeater1" Runat="server">
52.
53. <ItemTemplate>
54.
55. <tr>
56.
57. <td>
58.

59. <%
# DataBinder.Eval(Container, "DataItem.FVoteName") %>
60.
61. <asp:Repeater ID="rep" Runat="server">
62.
63. <ItemTemplate>
64.
65. <table width="85%" align="center" border="1" bordercolor="#D6E7FF" cellpadding="0" cellspacing="0">
66.
67. <tr>
68.
69. <td width="10%">
70.
71. 选项:
72.
73. </td width="20%">
74.

75. <td><%
# DataBinder.Eval(Container, "DataItem.FItemName") %></td>
76.
77. <td width="10%">票数:</td>
78.

79. <td width="10%"><%
# DataBinder.Eval(Container, "DataItem.FItemNum") %></td>
80.
81. <td width="10%">百分比:</td>
82.

83. <td width="40%"><img src="images/654.gif" height="5" width="<%# IsViews(DataBinder.Eval(Container, "DataItem.FVoteId").ToString(),DataBinder.Eval(Container, "DataItem.FItemNum").ToString()) %>"><%
# IsView(DataBinder.Eval(Container, "DataItem.FVoteId").ToString(),DataBinder.Eval(Container, "DataItem.FItemNum").ToString()) %></td>
84.
85. </tr>
86.
87. </table>
88.
89. </ItemTemplate>
90.
91. </asp:Repeater>
92.
93. </td>
94.
95. </tr>
96.
97. </ItemTemplate>
98.
99. </asp:repeater></table>
100.
101. </td>
102.
103. </tr>
104.
105. <tr>
106.
107. <td align="center"></td>
108.
109. </tr>
110.
111. </tbody>
112.
113. </table>
114.
115. </div>
116.
117. <div align="right"><FONT face="宋体"></FONT></div>
118.
119. </td>
120.
121. </tr>
122.
123. </tbody>
124.
125. </table>
4,后台代码
1. protected System.Web.UI.WebControls.Repeater Repeater1;
2.
3.
4.
5. public Seaskyer.Modules.Utils.DBClass db = new Seaskyer.Modules.Utils.DBClass();
6.
7.
8.
9. public string Title;
10.
11.
12.
13. //public string cid;
14.
15.
16.
17. private void Page_Load(object sender, System.EventArgs e)
18.

19.
{
20.
21. if(!Page.IsPostBack)
22.

23.
{
24.
25. string cid = "";
26.
27. if(db.q("id")!="")
28.

29.
{
30.
31. cid=db.q("id");
32.
33. }
34.
35.
36.
37. Title = db.getSingleValue("cms_VotePro","FProName","FID",cid);
38.
39.
40.
41. BindRep();
42.
43. }
44.
45. }
46.
47.
48.

49. /**//// <summary>
50.

51. /**//// 绑定Repeater控件,显示调查中的大类
52.

53. /**//// </summary>
54.
55. public void BindRep()
56.

57.
{
58.
59. string strsql = "select * from cms_vote where FProID='"+db.q("id")+"'";
60.
61. db.dp.CommandText = strsql;
62.
63. DataTable dt = db.dp.DataTableSQL();
64.
65. if(dt.Rows.Count>0)
66.

67.
{
68.
69. this.Repeater1.DataSource = dt;
70.
71. this.Repeater1.DataBind();
72.
73. }
74.
75. }
76.
77.
78.

79. Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
80.
81. override protected void OnInit(EventArgs e)
82.

83.
{
84.
85. //
86.
87. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
88.
89. //
90.
91. InitializeComponent();
92.
93. base.OnInit(e);
94.
95. }
96.
97.
98.

99. /**//// <summary>
100.

101. /**//// 设计器支持所需的方法 - 不要使用代码编辑器修改
102.

103. /**//// 此方法的内容。
104.

105. /**//// </summary>
106.
107. private void InitializeComponent()
108.

109.
{
110.
111. this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
112.
113. this.Load += new System.EventHandler(this.Page_Load);
114.
115.
116.
117. }
118.
119. #endregion
120.
121.
122.
123. private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
124.

125.
{
126.
127. Repeater rep = (Repeater)e.Item.FindControl("rep");
128.
129.
130.
131. DataRowView rowv = (DataRowView)e.Item.DataItem;
132.
133. //提取分类ID
134.
135. string Fid = rowv["FVoteId"].ToString();
136.
137.
138.
139. string strsql = "select * from cms_VoteItem where FVoteId='"+Fid+"'";
140.
141.
142.
143.
144.
145.
146.
147. //Response.Write("<script>alert('"+strsql+"');</script>");
148.
149. db.dp.CommandText = strsql;
150.
151. DataTable dt = db.dp.DataTableSQL();
152.
153.
154.
155. rep.DataSource = dt;
156.
157. rep.DataBind();
158.
159. }
160.
161.
162.
163.
164.
165. public string IsView(string cid,string num)
166.

167.
{
168.
169. string strsql = "select sum(FItemNum) from cms_VoteItem where FVoteId='"+cid+"'";
170.
171. db.dp.CommandText = strsql;
172.
173. DataTable dt = db.dp.DataTableSQL();
174.
175. double Sums = Convert.ToInt32(dt.Rows[0][0].ToString());
176.
177. if(num=="0")
178.
179. return "0%";
180.
181. else
182.
183. return (Convert.ToDouble(num)/Sums).ToString("P");
184.
185. }
186.
187.
188.
189. public string IsViews(string cid,string num)
190.

191.
{
192.
193. string strsql = "select sum(FItemNum) from cms_VoteItem where FVoteId='"+cid+"'";
194.
195. db.dp.CommandText = strsql;
196.
197. DataTable dt = db.dp.DataTableSQL();
198.
199. double Sums = Convert.ToInt32(dt.Rows[0][0].ToString());
200.
201. if(num=="0")
202.
203. return "0%";
204.
205. else
206.
207. return (Convert.ToDouble(num)/Sums*300).ToString();
208.
209. }
其中的表cms_VotePro是记录复式的主标题
cms_vote是记录投票项
cms_VoteItem是最后的小项了