Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic CheckBox Ctrl - what is wrong with code?

Dynamic CheckBox Ctrl - what is wrong with code?

Scheduled Pinned Locked Moved ASP.NET
questioncsharpdatabasecombeta-testing
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xsoftdev2
    wrote on last edited by
    #1

    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_Load private 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("

    J U 2 Replies Last reply
    0
    • X xsoftdev2

      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_Load private 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("

      J Offline
      J Offline
      Javier Lozano
      wrote on last edited by
      #2

      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 a System.Web.UI.WebControls.PlaceHolder control. They handle the adding of dymanic controls a little bit better. ~Javier Lozano (blog)

      X 1 Reply Last reply
      0
      • J Javier Lozano

        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 a System.Web.UI.WebControls.PlaceHolder control. They handle the adding of dymanic controls a little bit better. ~Javier Lozano (blog)

        X Offline
        X Offline
        xsoftdev2
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • X xsoftdev2

          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_Load private 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("

          U Offline
          U Offline
          utsav_verma
          wrote on last edited by
          #4

          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

          X 1 Reply Last reply
          0
          • U utsav_verma

            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

            X Offline
            X Offline
            xsoftdev2
            wrote on last edited by
            #5

            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.

            U 1 Reply Last reply
            0
            • X xsoftdev2

              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.

              U Offline
              U Offline
              utsav_verma
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups