asp.net中repeater嵌套CheckBoxList,RadioButtonList来完成投票

我们在做复式投票的时候。会遇到标题和选项嵌套的样子,下面我把我的代码放上来。 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="../../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,后台代码
protected System.Web.UI.WebControls.Repeater Repeater1;



		public Seaskyer.Modules.Utils.DBClass db = new Seaskyer.Modules.Utils.DBClass();

		protected System.Web.UI.WebControls.Button btnAdd;



		public string Title;

		//public string cid;

	

		private void Page_Load(object sender, System.EventArgs e)

		{

			if(!Page.IsPostBack)

			{

                string cid = "";

				if(db.q("id")!="")

				{

                    cid=db.q("id");

				}





                Title = db.getSingleValue("cms_VotePro","FProName","FID",cid);



				BindRep();

			}

		}



		/// <summary>

		/// 绑定Repeater控件,显示调查中的大类

		/// </summary>

		public void BindRep()

		{

            string strsql = "select * from cms_vote where FProID='"+db.q("id")+"'";

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();

			if(dt.Rows.Count>0)

			{

                this.Repeater1.DataSource = dt;

				this.Repeater1.DataBind();

			}

		}

		







		#region Web 窗体设计器生成的代码

		override protected void OnInit(EventArgs e)

		{

			//

			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

			//

			InitializeComponent();

			base.OnInit(e);

		}

		

		/// <summary>

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

		/// 此方法的内容。

		/// </summary>

		private void InitializeComponent()

		{    

			this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);

			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);

			this.Load += new System.EventHandler(this.Page_Load);



		}

		#endregion



		private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)

		{

			CheckBoxList cb    = (CheckBoxList)e.Item.FindControl("cb");

			RadioButtonList rb = (RadioButtonList)e.Item.FindControl("rb");

			TextBox tb         = (TextBox)e.Item.FindControl("tb");

			System.Web.UI.HtmlControls.HtmlInputHidden hb = (HtmlInputHidden)e.Item.FindControl("hb");

			DataRowView rowv = (DataRowView)e.Item.DataItem;

			//提取分类ID

			string Fid  = rowv["FVoteId"].ToString();

			string type = rowv["FType"].ToString();



			string strsql = "select * from cms_VoteItem where FVoteId='"+Fid+"'";



			//Response.Write("<script>alert('"+strsql+"');</script>");

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();

			

			switch(Convert.ToInt32(type))

			{

                case 1:

					rb.Visible = true;

					cb.Visible = false;

					tb.Visible = false;

					rb.DataSource = dt;

					rb.DataTextField = "FItemName";

					rb.DataValueField = "FID";

					rb.DataBind();

					break;

				case 2:

					rb.Visible = false;

					cb.Visible = true;

					tb.Visible = false;

					cb.DataSource = dt;

					cb.DataTextField = "FItemName";

					cb.DataValueField = "FID";

					cb.DataBind();

					break;

				case 3:

                    tb.Visible = true;

                    

					rb.Visible = false;

					cb.Visible = false;

					break;

			}

		}



		private void btnAdd_Click(object sender, System.EventArgs e)

		{

			string cb = "";//复选框

			string rb = "";//单选框

			string tb = "";//如果有文本框

			foreach   (RepeaterItem   item   in   this.Repeater1.Items)    

			{  



				CheckBoxList cb1    = (CheckBoxList)item.FindControl("cb");

				RadioButtonList rb1 = (RadioButtonList)item.FindControl("rb");

				TextBox tb1         = (TextBox)item.FindControl("tb");



				//CheckBox cb =   (CheckBox)item.FindControl("CheckBoxRole");  

				if(cb1.Visible==true)

				{

					for(int i=0;i<cb1.Items.Count;i++)

					{

						if(cb1.Items[i].Selected==true)

						{

							cb += "'"+cb1.Items[i].Value+"'" + ",";

						}

					}

				}

  

				if(rb1.Visible==true)

				{

					for(int i=0;i<rb1.Items.Count;i++)

					{

						if(rb1.Items[i].Selected==true)

						{

                            rb = "'"+rb1.Items[i].Value+"'";

						}

					}

				}



				if(tb1.Visible==true)

				{

					tb = tb1.Text + "|";

				}

			}



			string id = "";



			if(cb.Trim()!="")

			{

                cb = cb.Substring(0,cb.Length-1);

                id = cb;

			}



			if(rb.Trim()!="")

			{

				if(id.Trim()!="")

				{

					id += "," + rb;

				}

				else

				{

					id += rb;

				}

			}

            //id = cb + rb;



			if(id.Trim()=="")

			{

                Response.Write("<script>alert('你没有进行选择!');history.go(-1);</script>");

			}

			string strsql = "update cms_VoteItem set FItemNum=FItemNum+1 where FID in ("+id+")";

			db.dp.CommandText = strsql;

			db.dp.ExecuteNonQuery();

			Response.Write("<script>alert('投票结束!');location.href='VoteView.aspx?id="+db.q("id")+"';</script>");

//			Response.Write(id);

		}
3,显示投票结果前台
<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="../../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") %>

																	<asp:Repeater ID="rep" Runat="server">

																		<ItemTemplate>

																			<table width="85%" align="center" border="1" bordercolor="#D6E7FF" cellpadding="0" cellspacing="0">

																				<tr>

																					<td width="10%">

																						选项:

																					</td width="20%">

																					<td><%# DataBinder.Eval(Container, "DataItem.FItemName") %></td>

																					<td width="10%">票数:</td>

																					<td width="10%"><%# DataBinder.Eval(Container, "DataItem.FItemNum") %></td>

																					<td width="10%">百分比:</td>

																					<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>

																				</tr>

																			</table>

																		</ItemTemplate>

																	</asp:Repeater>

																</td>

															</tr>

														</ItemTemplate>

													</asp:repeater></table>

											</td>

										</tr>

										<tr>

											<td align="center"></td>

										</tr>

									</tbody>

								</table>

							</div>

							<div align="right"><FONT face="宋体"></FONT></div>

						</td>

					</tr>

				</tbody>

			</table>
4,后台代码
protected System.Web.UI.WebControls.Repeater Repeater1;



		public Seaskyer.Modules.Utils.DBClass db = new Seaskyer.Modules.Utils.DBClass();



		public string Title;



		//public string cid;

	

		private void Page_Load(object sender, System.EventArgs e)

		{

			if(!Page.IsPostBack)

			{

				string cid = "";

				if(db.q("id")!="")

				{

					cid=db.q("id");

				}



				Title = db.getSingleValue("cms_VotePro","FProName","FID",cid);



				BindRep();

			}

		}



		/// <summary>

		/// 绑定Repeater控件,显示调查中的大类

		/// </summary>

		public void BindRep()

		{

			string strsql = "select * from cms_vote where FProID='"+db.q("id")+"'";

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();

			if(dt.Rows.Count>0)

			{

				this.Repeater1.DataSource = dt;

				this.Repeater1.DataBind();

			}

		}



		#region Web 窗体设计器生成的代码

		override protected void OnInit(EventArgs e)

		{

			//

			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

			//

			InitializeComponent();

			base.OnInit(e);

		}

		

		/// <summary>

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

		/// 此方法的内容。

		/// </summary>

		private void InitializeComponent()

		{    

			this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);

			this.Load += new System.EventHandler(this.Page_Load);



		}

		#endregion



		private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)

		{

		    Repeater rep  = (Repeater)e.Item.FindControl("rep");



			DataRowView rowv = (DataRowView)e.Item.DataItem;

			//提取分类ID

			string Fid  = rowv["FVoteId"].ToString();



			string strsql = "select * from cms_VoteItem where FVoteId='"+Fid+"'";



			



			//Response.Write("<script>alert('"+strsql+"');</script>");

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();



			rep.DataSource = dt;

			rep.DataBind();

		}





		public string IsView(string cid,string num)

		{

            string strsql = "select sum(FItemNum) from cms_VoteItem where FVoteId='"+cid+"'";

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();

			double Sums = Convert.ToInt32(dt.Rows[0][0].ToString());

			if(num=="0")

				return "0%";

			else

			    return (Convert.ToDouble(num)/Sums).ToString("P");

		}



		public string IsViews(string cid,string num)

		{

			string strsql = "select sum(FItemNum) from cms_VoteItem where FVoteId='"+cid+"'";

			db.dp.CommandText = strsql;

			DataTable dt = db.dp.DataTableSQL();

			double Sums = Convert.ToInt32(dt.Rows[0][0].ToString());

			if(num=="0")

				return "0%";

			else

				return (Convert.ToDouble(num)/Sums*300).ToString();

		}
其中的表cms_VotePro是记录复式的主标题 cms_vote是记录投票项 cms_VoteItem是最后的小项了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值