' id='' value='' /> |
public partial class Quiz : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindEmployeeData();
Repeater();
}
}
private void BindEmployeeData()
{
string connection = ConfigurationManager.AppSettings["Connection"].ToString();
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand objCommand = new SqlCommand("SELECT * FROM t_Question", con))
{
using (SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objCommand))
{
DataTable dt = new DataTable();
dt.Columns.Add("QId");
dt.Columns.Add("QText");
objSqlDataAdapter.Fill(dt);
Repeater0.DataSource = dt;
Repeater0.DataBind();
}
}
}
}
private void Repeater()
{
for (int i = 0; i <= Repeater0.Items.Count - 1; i++)
{
string connection = "server=sv01;database=testdb;uid=sa;password=mpx123";
Repeater Repeater1 = (Repeater)Repeater0.Items[i].FindControl("Repeater1");
HiddenField QID = (HiddenField)Repeater0.Items[i].FindControl("QID");
SqlConnection ak = new SqlConnection(connection);
ak.Open();
string query = "Select * from [dbo].[t_Answers] where QId=" + QID.Value;
SqlDataAdapter adp= new SqlDataAdapter(query, ak);
DataTable ck = new DataTable();
//ck.Columns.Add("AOptions");
adp.Fill(ck);
Repeater1.DataSource = ck;
Repeater1.DataBind();
ak.Close();
}
}
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var collection = Repeater0.Items.Count;
foreach (var item in collection)
{
}
}
}
I have used two repeater in this, one for question,question Id and other for radio button to get data from database, but now i don't know how to save data that every radio button consist whenever user gonna check that option .
What I have tried:
I am trying for-each loop but don't know how to use it with nested repeater
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var collection = Repeater0.Items.Count;
foreach (var item in collection)
{
}
}
解决方案
Simplest way is to read the answers from the Request. The "name" of your radio buttons are the question ID so if you Request.Form[QId] you'll get the selected answer
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var collection = Repeater0.Items;
foreach (RepeaterItem item in collection)
{
HiddenField QID = (HiddenField)item.FindControl("QID");
int q = int.Parse(QID.Value);
string answer = Request.Form[q.ToString()];
}
}
An alternative would be to use server-side radio buttons, or a radiobuttonlist, and access those controls programmatically.