Dynamic CheckBox Ctrl - what is wrong with code?
-
Hello folks, I am exhausted with trouble shooting my code and can't seem to figure out why when the form is submited the Radio and CheckBox items get added again. If I submit the form 3 times I get 3 sets of Radio and CheckBoxes. I would appreciate any help and if I am going about creating these Checkboxes and radio buttons correctly. I am new to .Net Thanks Mike :confused: Here is the Database Diagram[^] global.asax
protected void Application_Start(Object sender, EventArgs e) { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionStringSQL"]); SqlCommand myCommand = new SqlCommand("SELECT Q.QuestionID, Q.Question, Q.QuestionType, Q.AnswerType FROM tblQuestion AS Q", myConnection); myCommand.CommandType = CommandType.Text; myConnection.Open(); SqlDataReader mydataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); ArrayList questionList = new ArrayList(); while (mydataReader.Read()) { object[] values = new object[mydataReader.FieldCount]; mydataReader.GetValues(values); questionList.Add(values); } Application["QuestionList"] = questionList; mydataReader.Close(); }
Page_Loadprivate void Page_Load(object sender, System.EventArgs e) { createLargeForm(); }
createLargeForm()private void createLargeForm() { ArrayList questionList = Application["QuestionList"] as ArrayList; SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionStringSQL"]); SqlDataAdapter myCommand = new SqlDataAdapter("spGetAnswerList", myConnection); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet dsAns = new DataSet("Answers"); myCommand.FillSchema(dsAns,SchemaType.Source, "Answers"); myCommand.Fill(dsAns,"Answers"); DataTable tblAnswers = dsAns.Tables["Answers"]; // End Get AnswerList pnlAdvSearch.Controls.Add(new LiteralControl("")); foreach(object[] row in questionList) { pnlAdvSearch.Controls.Add(new LiteralControl(" ")); pnlAdvSearch.Controls.Add(new LiteralControl(row[1].ToString())); // Write the Question pnlAdvSearch.Controls.Add(new LiteralControl("-QesType- " + row[2].ToString())); // Write Answertype pnlAdvSearch.Controls.Add(new LiteralControl("
-
Hello folks, I am exhausted with trouble shooting my code and can't seem to figure out why when the form is submited the Radio and CheckBox items get added again. If I submit the form 3 times I get 3 sets of Radio and CheckBoxes. I would appreciate any help and if I am going about creating these Checkboxes and radio buttons correctly. I am new to .Net Thanks Mike :confused: Here is the Database Diagram[^] global.asax
protected void Application_Start(Object sender, EventArgs e) { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionStringSQL"]); SqlCommand myCommand = new SqlCommand("SELECT Q.QuestionID, Q.Question, Q.QuestionType, Q.AnswerType FROM tblQuestion AS Q", myConnection); myCommand.CommandType = CommandType.Text; myConnection.Open(); SqlDataReader mydataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); ArrayList questionList = new ArrayList(); while (mydataReader.Read()) { object[] values = new object[mydataReader.FieldCount]; mydataReader.GetValues(values); questionList.Add(values); } Application["QuestionList"] = questionList; mydataReader.Close(); }
Page_Loadprivate void Page_Load(object sender, System.EventArgs e) { createLargeForm(); }
createLargeForm()private void createLargeForm() { ArrayList questionList = Application["QuestionList"] as ArrayList; SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionStringSQL"]); SqlDataAdapter myCommand = new SqlDataAdapter("spGetAnswerList", myConnection); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet dsAns = new DataSet("Answers"); myCommand.FillSchema(dsAns,SchemaType.Source, "Answers"); myCommand.Fill(dsAns,"Answers"); DataTable tblAnswers = dsAns.Tables["Answers"]; // End Get AnswerList pnlAdvSearch.Controls.Add(new LiteralControl("")); foreach(object[] row in questionList) { pnlAdvSearch.Controls.Add(new LiteralControl(" ")); pnlAdvSearch.Controls.Add(new LiteralControl(row[1].ToString())); // Write the Question pnlAdvSearch.Controls.Add(new LiteralControl("-QesType- " + row[2].ToString())); // Write Answertype pnlAdvSearch.Controls.Add(new LiteralControl("
Hey Mike, Do you want to want to run the code at every post back? Try wrapping the call to
createLargeForm()
around the following:if(!IsPostBack)
{
createLargeForm();
}Also, instead of using a Panel,
pnlAdvSearch
use aSystem.Web.UI.WebControls.PlaceHolder
control. They handle the adding of dymanic controls a little bit better. ~Javier Lozano (blog) -
Hey Mike, Do you want to want to run the code at every post back? Try wrapping the call to
createLargeForm()
around the following:if(!IsPostBack)
{
createLargeForm();
}Also, instead of using a Panel,
pnlAdvSearch
use aSystem.Web.UI.WebControls.PlaceHolder
control. They handle the adding of dymanic controls a little bit better. ~Javier Lozano (blog)Hello Javier, The reason I am not using if(!IsPostBack) is that for now I want to be able to view the form and see what data it is submitting, for debugging. Also, I want to make visible a results PlaceHolder to show results of my form and I want to Hide the results PlaceHolder and make visible the Form Placehholder when the user clicks on a link, all on the same page. Hope I am making clear what I am trying to do. Is this the most efficient way of doing this? I would certainly apprciate some feedback. Thanks Mike
-
Hello folks, I am exhausted with trouble shooting my code and can't seem to figure out why when the form is submited the Radio and CheckBox items get added again. If I submit the form 3 times I get 3 sets of Radio and CheckBoxes. I would appreciate any help and if I am going about creating these Checkboxes and radio buttons correctly. I am new to .Net Thanks Mike :confused: Here is the Database Diagram[^] global.asax
protected void Application_Start(Object sender, EventArgs e) { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionStringSQL"]); SqlCommand myCommand = new SqlCommand("SELECT Q.QuestionID, Q.Question, Q.QuestionType, Q.AnswerType FROM tblQuestion AS Q", myConnection); myCommand.CommandType = CommandType.Text; myConnection.Open(); SqlDataReader mydataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); ArrayList questionList = new ArrayList(); while (mydataReader.Read()) { object[] values = new object[mydataReader.FieldCount]; mydataReader.GetValues(values); questionList.Add(values); } Application["QuestionList"] = questionList; mydataReader.Close(); }
Page_Loadprivate void Page_Load(object sender, System.EventArgs e) { createLargeForm(); }
createLargeForm()private void createLargeForm() { ArrayList questionList = Application["QuestionList"] as ArrayList; SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionStringSQL"]); SqlDataAdapter myCommand = new SqlDataAdapter("spGetAnswerList", myConnection); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; DataSet dsAns = new DataSet("Answers"); myCommand.FillSchema(dsAns,SchemaType.Source, "Answers"); myCommand.Fill(dsAns,"Answers"); DataTable tblAnswers = dsAns.Tables["Answers"]; // End Get AnswerList pnlAdvSearch.Controls.Add(new LiteralControl("")); foreach(object[] row in questionList) { pnlAdvSearch.Controls.Add(new LiteralControl(" ")); pnlAdvSearch.Controls.Add(new LiteralControl(row[1].ToString())); // Write the Question pnlAdvSearch.Controls.Add(new LiteralControl("-QesType- " + row[2].ToString())); // Write Answertype pnlAdvSearch.Controls.Add(new LiteralControl("
Hi mike I pointed one place which shld b d root 4 error, look at the comments inserted by me.
foreach (DataRow drCurrent in tblAnswers.Rows) { if (row[0].Equals(drCurrent["QuestionID"])) { rb.Items.Add(new ListItem(drCurrent["AnswerChoice"].ToString(), drCurrent["AnswerID"].ToString())); } //Y R U DOING THIS pnlAdvSearch.Controls.Add(rb); } } // Radio //SUGGESTED HERE //pnlAdvSearch.Controls.Add(rb);
I by the way u r doing, there will be a radio button list/CHECK BOX list for every row in the database. Check me if i m wrong since i didnt compile my thought UTSAV -
Hi mike I pointed one place which shld b d root 4 error, look at the comments inserted by me.
foreach (DataRow drCurrent in tblAnswers.Rows) { if (row[0].Equals(drCurrent["QuestionID"])) { rb.Items.Add(new ListItem(drCurrent["AnswerChoice"].ToString(), drCurrent["AnswerID"].ToString())); } //Y R U DOING THIS pnlAdvSearch.Controls.Add(rb); } } // Radio //SUGGESTED HERE //pnlAdvSearch.Controls.Add(rb);
I by the way u r doing, there will be a radio button list/CHECK BOX list for every row in the database. Check me if i m wrong since i didnt compile my thought UTSAVHello UTSAV,
foreach (DataRow drCurrent in tblAnswers.Rows) { if (row[0].Equals(drCurrent["QuestionID"])) { rb.Items.Add(new ListItem(drCurrent["AnswerChoice"].ToString(), drCurrent["AnswerID"].ToString())); } //Y R U DOING THIS // I am adding a listitem for the AnswerChoice, that is the question has an AnswerChoice associated with it. pnlAdvSearch.Controls.Add(rb); } } // Radio //SUGGESTED HERE //pnlAdvSearch.Controls.Add(rb);
This is a survey application where the Questions and List of possible answer choices are generated by code. Depending on the AnswerType field in DB a CheckBoxList or RadioButtonList is created for the possible answers of the question. Hope this makes it clear. -
Hello UTSAV,
foreach (DataRow drCurrent in tblAnswers.Rows) { if (row[0].Equals(drCurrent["QuestionID"])) { rb.Items.Add(new ListItem(drCurrent["AnswerChoice"].ToString(), drCurrent["AnswerID"].ToString())); } //Y R U DOING THIS // I am adding a listitem for the AnswerChoice, that is the question has an AnswerChoice associated with it. pnlAdvSearch.Controls.Add(rb); } } // Radio //SUGGESTED HERE //pnlAdvSearch.Controls.Add(rb);
This is a survey application where the Questions and List of possible answer choices are generated by code. Depending on the AnswerType field in DB a CheckBoxList or RadioButtonList is created for the possible answers of the question. Hope this makes it clear.Hi mike I still stuck with my point, may b i m wrong xsoftdev2 wrote: pnlAdvSearch.Controls.Add(rb); what i feel this particular line will add ur RadioButtonListControl 4 every answer, i think it'd solve ur problem, this is the only point where i see d bug(in respect of checkbox too), try it. UTSAV