<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("subjectcontent") %>'></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("subjectid") %>' />
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
</ItemTemplate>
</asp:DataList> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="看看选了什么答案" /></div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DBClient db = new DBClient("examinationmei2");
string sql = "select * from optionsubject order by subjectid";
DataTable dt = db.getSQLTable(sql, "dt");
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
对应的ItemDataBound事件:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButtonList rbl = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
HiddenField hf = (HiddenField)e.Item.FindControl("HiddenField1");
if ((rbl != null)&(hf != null))
{
string sql = "select subjectid,optionid, optioncontent from optiontab where subjectid='"+hf.Value.ToString().Trim()+"'";
rbl.DataTextField = "optioncontent";
rbl.DataValueField = "optionid";
DBClient db = new DBClient("examinationmei2");
DataTable dt = db.getSQLTable(sql, "dt");
rbl.DataSource = dt;
rbl.DataBind();
}
}
判断结果,采用遍历方法:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < DataList1.Items.Count; i++)
{
RadioButtonList rbl = (RadioButtonList)DataList1.Items[i].FindControl("RadioButtonList1");
if (rbl != null)
{
if (rbl.SelectedIndex != -1)
{
Response.Write("第" + i + "题:你选了<" + rbl.SelectedItem.Value.ToString() + ">-" + rbl.SelectedItem.Text.ToString() + "<br>");
}
else
{
Response.Write("第" + i + "题:<font color='red'>你没做。</font><br>");
}
}
}
}
| ©2003-2012 Woody. Some Rights Reserved. Feed - Sitemap - Valid XHTML - Valid CSS - Creative Commons Powered by Woody. Skin Reserved by Default | Processed in 0.016602 Seconds. 4 Queries | 浙ICP备07029590号 |