I am trying to create a form for courses provided by a University. Based on the no of subjects obtained through a query string, a form gets generated. If the subject has electives, the user has to check a checkbox, which further generates a no of textboxes for subject names. But whenever I check the checkbox of say 2nd subject the textboxes previously displayed for 1st subject disappear.

ASP CODE

<html>
 <body>
  <form id="form1" runat="server">
   <div>
   </div>
  </form> 
 </body>
</html>

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Demo : System.Web.UI.Page
{

    #region variables
    Label[] lb_subject_names;
    TextBox[] tb_subject_names;

    Label[] lb_elective;

    Table[] table_electives;

    CheckBox[] cb_elective;
    TextBox[] tb_no_of_elective;
    Button[] bt_electives_ok;

    TextBox[][] tb_name_of_electives;
    TextBox[][] tb_code_of_electives;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        int no_subjects = 3;// int.Parse(Request.QueryString["NoSubjects"]);

        //declare
        lb_subject_names = new Label[no_subjects];
        tb_subject_names = new TextBox[no_subjects];

        lb_elective = new Label[no_subjects];

        table_electives = new Table[no_subjects];

        cb_elective = new CheckBox[no_subjects];
        tb_no_of_elective = new TextBox[no_subjects];
        bt_electives_ok = new Button[no_subjects];

        tb_name_of_electives = new TextBox[no_subjects][];
        tb_code_of_electives = new TextBox[no_subjects][];

        //initialize
        for (int i = 0; i < no_subjects; i++)
        {

            lb_subject_names[i] = new Label();
            tb_subject_names[i] = new TextBox();

            lb_elective[i] = new Label();

            table_electives[i] = new Table();
            table_electives[i].BorderStyle = BorderStyle.Solid;

            tb_no_of_elective[i] = new TextBox();

            cb_elective[i] = new CheckBox();
            cb_elective[i].AutoPostBack = true;
            cb_elective[i].ID = (i).ToString();
            cb_elective[i].CheckedChanged += new EventHandler(cb_elective_CheckedChanged);

            bt_electives_ok[i] = new Button();
            bt_electives_ok[i].Text = "OK";
            bt_electives_ok[i].ID = "bt_" + (i).ToString();
            bt_electives_ok[i].Click += new EventHandler(bt_electives_ok_clicked);


        }

        //display in form1
        for (int i = 0; i < no_subjects; i++)
        {

            lb_subject_names[i].Text = "<br/>Subject Name : ";
            form1.Controls.Add(lb_subject_names[i]);
            form1.Controls.Add(tb_subject_names[i]);

            lb_elective[i].Text = "Does the Subject provide Electives ?";
            form1.Controls.Add(lb_elective[i]);
            form1.Controls.Add(cb_elective[i]);
            tb_no_of_elective[i].Visible = false;
            bt_electives_ok[i].Visible = false;
            form1.Controls.Add(tb_no_of_elective[i]);
            form1.Controls.Add(bt_electives_ok[i]);

            form1.Controls.Add(table_electives[i]);


        }

    }

    protected void cb_elective_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox currentCheckbox = sender as CheckBox;
        int id = int.Parse(currentCheckbox.ID);

        if (currentCheckbox != null && currentCheckbox.Checked)
        {
            tb_no_of_elective[id].Visible = true;
            bt_electives_ok[id].Visible = true;
            table_electives[id].Visible = true;
        }
        else
        {
            tb_no_of_elective[id].Visible = false;
            bt_electives_ok[id].Visible = false;
            table_electives[id].Visible = false;
        }

    }

    protected void bt_electives_ok_clicked(object sender, EventArgs e)
    {

        Button button = sender as Button;
        String str_id = button.ID;
        int id = int.Parse(str_id.Substring(3, str_id.Length - 3));

        int no_of_electives = int.Parse(tb_no_of_elective[id].Text);

        tb_name_of_electives[id] = new TextBox[no_of_electives];
        tb_code_of_electives[id] = new TextBox[no_of_electives];

        for (int i = 0; i < no_of_electives; i++)
        {
            tb_name_of_electives[id][i] = new TextBox();
            tb_code_of_electives[id][i] = new TextBox();
        }

        //row1 header
        TableRow e_row1 = new TableRow();
        TableCell e_cell1_1 = new TableCell();
        e_cell1_1.Text = "Sr. No.";
        e_row1.Controls.Add(e_cell1_1);

        TableCell e_cell1_2 = new TableCell();
        e_cell1_2.Text = "Subject Code";
        e_row1.Controls.Add(e_cell1_2);

        TableCell e_cell1_3 = new TableCell();
        e_cell1_3.Text = "Subject Name";
        e_row1.Controls.Add(e_cell1_3);

        table_electives[id].Controls.Add(e_row1);

        for (int i = 0; i < no_of_electives; i++)
        {
            //row2
            TableRow e_row2 = new TableRow();
            TableCell e_cell2_1 = new TableCell();
            e_cell2_1.Text = (i + 1).ToString();
            e_row2.Controls.Add(e_cell2_1);

            TableCell e_cell2_2 = new TableCell();
            e_cell2_2.Controls.Add(tb_code_of_electives[id][i]);
            e_row2.Controls.Add(e_cell2_2);

            TableCell e_cell2_3 = new TableCell();
            e_cell2_3.Controls.Add(tb_name_of_electives[id][i]);
            e_row2.Controls.Add(e_cell2_3);

            table_electives[id].Controls.Add(e_row2);
        }

    }

}

Related posts

Recent Viewed