asp.net logic error-Multiple controls with the same ID 'cbk1' were found.
Please help me with the code.
error:
Multiple controls with the same ID 'cbk1' were found. FindControl requires that controls have unique IDs.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
public partial class xDefault : System.Web.UI.Page
{
public int tNUM;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=ADMIN-B2C44FCB4\\SQLEXPRESS;Initial Catalog=examfeb8;Integrated Security=True");
cn.Open();
SqlCommand c = new SqlCommand("select * from stud where snum='" + Session["id"].ToString() + "'", cn);
SqlDataReader r = c.ExecuteReader();
r.Read();
lblStuName.Text = r["sname"].ToString();
Session["sname"] = lblStuName.Text;
//string q = lblStuName.Text;
cn.Close();
DetailsView1.DataBind();
getOption();
}
public void getOption()
{
int i = 1;
SqlConnection con = new SqlConnection("Data Source=ADMIN-B2C44FCB4\\SQLEXPRESS;Initial Catalog=examfeb8;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("select top 1 * from Quest where Course='" + Session["Course"].ToString() + "'", con);
SqlDataReader sdr = cmd1.ExecuteReader();
while (sdr.Read())
{
RadioButtonList cbk = new RadioButtonList();
cbk.ID = "cbk" + i.ToString();
cbk.Items.Add("A. " + Server.HtmlEncode(sdr["opt1"].ToString()));
cbk.Items.Add("B. " + Server.HtmlEncode(sdr["opt2"].ToString()));
cbk.Items.Add("C. " + Server.HtmlEncode(sdr["opt3"].ToString()));
cbk.Items.Add("D. " + Server.HtmlEncode(sdr["opt4"].ToString()));
cbk.Font.Size = 11;
for (int j = 1; j <= 4; j++)
{
cbk.Items[j - 1].Value = j.ToString();
}
Panel2.Controls.Add(cbk);
i++;
tNUM++;
}
sdr.Close();
con.Close();
}
protected void btnNxt_Click(object sender, EventArgs e)
{
getOption();
//---------
string msc = "";
for (int i = 1; i<= tNUM; i++)
{
RadioButtonList list = (RadioButtonList)Panel2.FindControl("cbk" + i.ToString());
if (list != null)
{
if (list.SelectedValue.ToString() != "")
{
msc += list.SelectedValue.ToString();
}
else
{
msc += "0";
}
}
}
//--------
//--------
System.Data.DataRowView dr = (System.Data.DataRowView)DetailsView1.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionOrder"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
//a.UserAns = answerDropDownList.SelectedValue.ToString();
a.UserAns = msc;
ArrayList al = (ArrayList)Session["AnswerList"];
al.Add(a);
Session.Add("AnswerList", al);
if (DetailsView1.PageIndex == DetailsView1.PageCount - 1)
{
// Go to evaluate answers
Response.Redirect("xresult.aspx");
}
else
{
DetailsView1.PageIndex++;
}
if (DetailsView1.PageIndex == DetailsView1.PageCount - 1)
{
btnNxt.Text = "Finished";
}
}
}
Re: asp.net logic error-Multiple controls with the same ID 'cbk1' were found.
please use CODE tags when posting code
By the way, I think your problem lies here :
Code:
RadioButtonList list = (RadioButtonList)Panel2.FindControl("cbk" + i.ToString
I'd have used JavaScript here to find a particular control. much easier and cleaner.