select_web.aspx页:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="select_web.aspx.cs" Inherits="select_web" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>选择题</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lab_wenjuantitle" runat="server" Text="问题的主题"></asp:Label>
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<%# Eval("timu_title") %>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
<br />
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
======================================================================================================
select_web.aspx.cs页:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class select_web : System.Web.UI.Page
{
int wenjuan_id,timu_shuliang;
public string wenjuan_title;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["wenjuan_id"] != null)
{
wenjuan_id = int.Parse(Request.QueryString["wenjuan_id"].ToString());
}
getWenjuan();
}
//获得问卷
public void getWenjuan()
{
selectQuestion sq = new selectQuestion();
DataSet ds = sq.GetTimuByWenjuan_id(wenjuan_id);
DataTable dt0 = ds.Tables[0];
if (dt0.Rows.Count > 0)
{
this.lab_wenjuantitle.Text = dt0.Rows[0]["wenjuan_title"].ToString();
}
DataTable dt1 = ds.Tables[1];
timu_shuliang = dt1.Rows.Count;//获得题目的数量,用于判断是否全部选择
this.Repeater1.DataSource = dt1;
this.Repeater1.DataBind();
}
//获得选项
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RadioButtonList rb = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
DataRowView rowv = (DataRowView)e.Item.DataItem;
string timu_id = rowv["timu_id"].ToString();
selectQuestion sq = new selectQuestion();
DataTable dt = sq.GetFensuByTimu_id(int.Parse(timu_id)).Tables[0];
if (dt.Rows.Count > 0)
{
rb.DataSource = dt;
rb.DataTextField = "timu_chooseContent";
rb.DataValueField = "fenshu_id";
rb.DataBind();
}
}
//处理选择的操作
protected void Button1_Click(object sender, EventArgs e)
{
string rb = "";//单选框
foreach (RepeaterItem item in this.Repeater1.Items)
{
RadioButtonList rb1 = (RadioButtonList)item.FindControl("RadioButtonList1");
for (int i = 0; i < rb1.Items.Count; i++)
{
if (rb1.Items[i].Selected == true)
{
if (rb.Trim() == "")
{
rb += rb1.Items[i].Value;
}
else
{
rb += "|" + rb1.Items[i].Value;
}
}
}
}
string[] s = rb.Split('|');
if (s.Length == timu_shuliang)//判断是否都选择了
{
Response.Write("<script>alert('" + rb + "');</script>");//处理结果
}
else
{
Response.Write("<script>alert('请做完所有题目!');</script>");
}
}
}