单选按钮列表问题 [英] Radiobutton List Issues
本文介绍了单选按钮列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在尝试进行在线测验.我把所有问题都放在数据库表中,我也是
能够成功地呈现这些问题并提供选项(答案).
但是当我使用
RadioButtonList1.Items.Selected/SelectedIndex/SelectedValue/SelectedItem ..
它返回-1或给出null异常.
我在哪里错了???
代码如下
Hi
I was trying to built online quiz. i put all the question in my DB table i was also
able to successfully render those question and there options (Answers).
But when i am using
RadioButtonList1.Items.Selected / SelectedIndex /SelectedValue / SelectedItem ..
it returns -1 or give null exception .
Where am i going wrong ???
Code is as below
public partial class testpage : System.Web.UI.Page
{
public DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("connectionString");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from table ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = "1." + dt.Rows[0]["Question"].ToString();
Label2.Text = "2." + dt.Rows[1]["Question"].ToString();
Label3.Text = "3." + dt.Rows[2]["Question"].ToString();
Label4.Text = "4." + dt.Rows[3]["Question"].ToString();
Label5.Text = "5." + dt.Rows[4]["Question"].ToString();
Label6.Text = "6." + dt.Rows[5]["Question"].ToString();
Label7.Text = "7." + dt.Rows[6]["Question"].ToString();
Label8.Text = "8." + dt.Rows[7]["Question"].ToString();
Label9.Text = "9." + dt.Rows[8]["Question"].ToString();
Label10.Text = "10." + dt.Rows[9]["Question"].ToString();
string[] list1 = { dt.Rows[0]["Answer1"].ToString(), dt.Rows[0]["Answer2"].ToString(), dt.Rows[0]["Answer3"].ToString(), dt.Rows[0]["Answer4"].ToString() };
string[] list2 = { dt.Rows[1]["Answer1"].ToString(), dt.Rows[1]["Answer2"].ToString(), dt.Rows[1]["Answer3"].ToString(), dt.Rows[1]["Answer4"].ToString() };
string[] list3 = { dt.Rows[2]["Answer1"].ToString(), dt.Rows[2]["Answer2"].ToString(), dt.Rows[2]["Answer3"].ToString(), dt.Rows[2]["Answer4"].ToString() };
string[] list4 = { dt.Rows[3]["Answer1"].ToString(), dt.Rows[3]["Answer2"].ToString(), dt.Rows[3]["Answer3"].ToString(), dt.Rows[3]["Answer4"].ToString() };
string[] list5 = { dt.Rows[4]["Answer1"].ToString(), dt.Rows[4]["Answer2"].ToString(), dt.Rows[4]["Answer3"].ToString(), dt.Rows[4]["Answer4"].ToString() };
string[] list6 = { dt.Rows[5]["Answer1"].ToString(), dt.Rows[5]["Answer2"].ToString(), dt.Rows[5]["Answer3"].ToString(), dt.Rows[5]["Answer4"].ToString() };
string[] list7 = { dt.Rows[6]["Answer1"].ToString(), dt.Rows[6]["Answer2"].ToString(), dt.Rows[6]["Answer3"].ToString() };
string[] list8 = { dt.Rows[7]["Answer1"].ToString(), dt.Rows[7]["Answer2"].ToString(), dt.Rows[7]["Answer3"].ToString(), dt.Rows[7]["Answer4"].ToString() };
string[] list9 = { dt.Rows[8]["Answer1"].ToString(), dt.Rows[8]["Answer2"].ToString(), dt.Rows[8]["Answer3"].ToString() };
string[] list10 = { dt.Rows[9]["Answer1"].ToString(), dt.Rows[9]["Answer2"].ToString(), dt.Rows[9]["Answer3"].ToString() };
RadioButtonList1.DataSource = list1;
RadioButtonList1.DataBind();
RadioButtonList2.DataSource = list2;
RadioButtonList2.DataBind();
RadioButtonList3.DataSource = list3;
RadioButtonList3.DataBind();
RadioButtonList4.DataSource = list4;
RadioButtonList4.DataBind();
RadioButtonList5.DataSource = list5;
RadioButtonList5.DataBind();
RadioButtonList6.DataSource = list6;
RadioButtonList6.DataBind();
RadioButtonList7.DataSource = list7;
RadioButtonList7.DataBind();
RadioButtonList8.DataSource = list8;
RadioButtonList8.DataBind();
RadioButtonList9.DataSource = list9;
RadioButtonList9.DataBind();
RadioButtonList10.DataSource = list10;
RadioButtonList10.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string result;
string[] Answer = { dt.Rows[0]["Correct_Answer"].ToString(),
dt.Rows[1]["Correct_Answer"].ToString(),
dt.Rows[2]["Correct_Answer"].ToString(),
dt.Rows[3]["Correct_Answer"].ToString(),
dt.Rows[4]["Correct_Answer"].ToString(),
dt.Rows[5]["Correct_Answer"].ToString(),
dt.Rows[6]["Correct_Answer"].ToString(),
dt.Rows[7]["Correct_Answer"].ToString(),
dt.Rows[8]["Correct_Answer"].ToString(),
dt.Rows[9]["Correct_Answer"].ToString() };
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[1].Selected)
{
result = result + 1;
}
}
//for (int i = 0; i < RadioButtonList2.Items.Count; i++)
//{
// if (RadioButtonList2.Items[Convert.ToInt32(Answer[1])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList3.Items.Count; i++)
//{
// if (RadioButtonList3.Items[Convert.ToInt32(Answer[2])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList4.Items.Count; i++)
//{
// if (RadioButtonList4.Items[Convert.ToInt32(Answer[3])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList5.Items.Count; i++)
//{
// if (RadioButtonList5.Items[Convert.ToInt32(Answer[4])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList6.Items.Count; i++)
//{
// if (RadioButtonList6.Items[Convert.ToInt32(Answer[5])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList7.Items.Count; i++)
//{
// if (RadioButtonList7.Items[Convert.ToInt32(Answer[6])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList8.Items.Count; i++)
//{
// if (RadioButtonList8.Items[Convert.ToInt32(Answer[7])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList9.Items.Count; i++)
//{
// if (RadioButtonList9.Items[Convert.ToInt32(Answer[8])].Selected)
// {
// result = result + 1;
// }
//}
//for (int i = 0; i < RadioButtonList10.Items.Count; i++)
//{
// if (RadioButtonList10.Items[Convert.ToInt32(Answer[9])].Selected)
// {
// result = result + 1;
// }
//}
//result = result * 10;
Response.Write(result);
}
}
}
推荐答案
我想我已经解决了你的问题.请尝试在(!Page.IsPostBack)中的页面加载中加载控件.例如-
Hi,
I think I figured out your problem. Please try to load your controls in Page load within (!Page.IsPostBack). For e.g. -
<div>
<asp:label id="Q1" runat="server" text="" ></asp:label>
<asp:radiobuttonlist id="RadioButtonList1" runat="server" >
</asp:radiobuttonlist>
<asp:label id="A1" runat="server" text="" ></asp:label><br />
<asp:label id="Q2" runat="server" text="" ></asp:label>
<asp:radiobuttonlist id="RadioButtonList2" runat="server" >
</asp:radiobuttonlist>
<asp:label id="A2" runat="server" text="" ></asp:label><br />
<asp:label id="Q3" runat="server" text="" ></asp:label>
<asp:radiobuttonlist id="RadioButtonList3" runat="server" >
</asp:radiobuttonlist>
<asp:label id="A3" runat="server" text="" ></asp:label><br />
<asp:button id="Button1" runat="server" text="Submit" onclick="Button1_Click" />
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtQs = PopulateQuestions();
if (dtQs.Rows.Count > 0)
{
Q1.Text = dtQs.Rows[0][0].ToString();
Q2.Text = dtQs.Rows[1][0].ToString();
Q3.Text = dtQs.Rows[2][0].ToString();
string[] ansQ1 = { dtQs.Rows[0]["Ans1"].ToString(), dtQs.Rows[0]["Ans2"].ToString(), dtQs.Rows[0]["Ans3"].ToString() };
string[] ansQ2 = { dtQs.Rows[1]["Ans1"].ToString(), dtQs.Rows[1]["Ans2"].ToString(), dtQs.Rows[1]["Ans3"].ToString() };
string[] ansQ3 = { dtQs.Rows[2]["Ans1"].ToString(), dtQs.Rows[2]["Ans2"].ToString(), dtQs.Rows[2]["Ans3"].ToString() };
RadioButtonList1.DataSource = ansQ1;
RadioButtonList1.DataBind();
RadioButtonList2.DataSource = ansQ2;
RadioButtonList2.DataBind();
RadioButtonList3.DataSource = ansQ3;
RadioButtonList3.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
A1.Text = RadioButtonList1.SelectedItem.Text;
A2.Text = RadioButtonList2.SelectedItem.Text;
A3.Text = RadioButtonList3.SelectedItem.Text;
}
这篇关于单选按钮列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文