5

Part of attendance tracker application which displays subjects of user and lets him mark his own attendance.

With enableviewstate="true", the checkbox does not get unchecked and the checkchanged event does not execute.

namespace portal

{

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

        String[] split; string day;
        protected void Page_Load(object sender, EventArgs e)
        {

            /* if (Session["uname"] == null)
             {
                 Response.Redirect("Firstpage.aspx");
             }*/
            String[] split = new String[16];
            DateTime d1 = DateTime.Today;
            string month = d1.ToString("MMMM");
            Lblmonth.Text = month;
            String userid = Session["uname"].ToString();
            SqlConnection con2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd2 = new SqlCommand("select sbranch ,syear from studdetails where userid=@userid ", con2);
            cmd2.Parameters.AddWithValue("userid", userid);
            String branch = "", year = "";
            SqlDataReader rdr2 = null;
            con2.Open();
            rdr2 = cmd2.ExecuteReader();
            while (rdr2.Read())
            {
                branch = rdr2["sbranch"].ToString();
                year = rdr2["syear"].ToString();
            }
            con2.Close();
            int sem = 0;
            switch (year)
            {
                case "B.E": sem = 8;
                    break;
                case "T.E": sem = 6;
                    break;
                case "S.E": sem = 4;
                    break;
                case "F.E": sem = 2;
                    break;
                default: sem = 2;
                    break;

            }

            int tempmonth = (int)(d1.Month);
            if (tempmonth > 6)
                sem = sem - 1;



            SqlConnection con3 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd3 = new SqlCommand("select subject from subjecttable where department=@dept and semester=@sem ", con3);
            cmd3.Parameters.AddWithValue("dept", branch);
            cmd3.Parameters.AddWithValue("sem", sem);
            con3.Open();
            SqlDataReader rdr3 = null;
            rdr3 = cmd3.ExecuteReader();
            string subject = "";
            while (rdr3.Read())
            {
                subject = rdr3["subject"].ToString();

            }
            con3.Close();
            Char[] separator = new Char[] { ',' };
            String[] subsplit = subject.Split(separator);
            int size = subsplit.Length;
            switch (size)
            {
                case 6: subj6.Text = subsplit[5];
                    goto case 5;
                case 5: subj5.Text = subsplit[4];
                    goto case 4;
                case 4: subj4.Text = subsplit[3];
                    goto case 3;
                case 3: subj3.Text = subsplit[2];
                    goto case 2;
                case 2: subj2.Text = subsplit[1];
                    goto case 1;
                case 1: subj1.Text = subsplit[0];
                    break; ;

            }

        }
        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsWeekend)
            {
                e.Cell.BackColor = System.Drawing.Color.DarkBlue;
            }
            else
            {
                e.Cell.BackColor = System.Drawing.Color.LightBlue;
            }
            SelectedDatesCollection dates = Calendar1.SelectedDates;

            DateTime toDay = DateTime.Today;
            string days = toDay.ToLongDateString();

            Labelday.Text = days;
            Char[] separator = new Char[] { ',' };
            String[] split1 = days.Split(separator);
            day = split1[0];
            Session["day"] = Calendar1.SelectedDate.ToShortDateString();
            if (dates.Count > 0)
            {
                Labelday.Text = dates[0].ToLongDateString() + "<br />";
                String temp = Labelday.Text;
                split1 = temp.Split(separator);
                day = split1[0];
            }
            if (!(day.Equals("Saturday") || day.Equals("Sunday")))
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                string cla = Convert.ToString(Session["uclass"]);
                SqlCommand cmd = new SqlCommand("select " + day + " from studtimetable where class = @class ", con);

                con.Open();
                cmd.Parameters.AddWithValue("class", cla);
                SqlDataReader rdr = null;
                String tt = "";
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    tt = rdr[day].ToString();
                }
                con.Close();

                split = tt.Split(separator);
                int size = split.Length;
                lbltime.Visible = true;
                Lblsubj.Visible = true;
                Lblatt.Visible = true;
                switch (size)
                {
                    case 16: Label15.Text = split[15];
                        Session["l15"] = Label15.Text;
                        Label14.Text = split[14];
                        CheckBox8.Visible = true;

                        goto case 14;
                    case 14: Label13.Text = split[13];
                        Session["l13"] = Label13.Text;
                        Label12.Text = split[12];
                        CheckBox7.Visible = true;
                        goto case 12;
                    case 12: Label11.Text = split[11];
                        Session["l11"] = Label11.Text;
                        Label10.Text = split[10];
                        CheckBox6.Visible = true;
                        goto case 10;
                    case 10: Label9.Text = split[9];
                        Session["l9"] = Label9.Text;
                        Label8.Text = split[8];
                        CheckBox5.Visible = true;
                        goto case 8;
                    case 8: Label7.Text = split[7];
                        Session["l7"] = Label7.Text;
                        Label6.Text = split[6];
                        CheckBox4.Visible = true;
                        goto case 6;
                    case 6: Label5.Text = split[5];
                        Session["l5"] = Label5.Text;
                        Label4.Text = split[4];
                        CheckBox3.Visible = true;
                        goto case 4;
                    case 4: Label3.Text = split[3];
                        Session["l3"] = Label3.Text;
                        Label2.Text = split[2];
                        CheckBox2.Visible = true;
                        goto case 2;
                    case 2: Label1.Text = split[1];
                        Session["l1"] = Label1.Text;
                        Label0.Text = split[0];
                        CheckBox1.Visible = true;
                        break;

                }

            }
            else
            {
                lbltime.Text = "Enjoy weekend";
                lbltime.Visible = true;
            }
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {

            CheckBox1.Checked = false;

            CheckBox2.Checked = false;

            CheckBox3.Checked = false;

            CheckBox4.Checked = false;

            CheckBox5.Checked = false;

            CheckBox6.Checked = false;

            CheckBox7.Checked = false;

            CheckBox8.Checked = false;
        }



        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                string sub = Session["l1"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();


            }
            else
            {
                Label18.Text = "uncheck";//temporary to check if this block executes
            }
        }



        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {

            if (CheckBox2.Checked == true)
            {
                string sub = Session["l3"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }

        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox3.Checked == true)
            {
                string sub = Session["l5"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }

        }

        protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox4.Checked == true)
            {
                string sub = Session["l7"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {

            }

        }

        protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox5.Checked == true)
            {
                string sub = Session["l9"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }
        }

        protected void CheckBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox6.Checked == true)
            {
                string sub = Session["l11"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }
        }

        protected void CheckBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox7.Checked == true)
            {
                string sub = Session["l13"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }

        }

        protected void CheckBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox8.Checked == true)
            {
                string sub = Session["l15"].ToString();
                string userid = Session["uname"].ToString();
                string date = Session["day"].ToString();
                string tempclass = Session["uclass"].ToString();

                SqlConnection con4 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\mmm\portal\App_Data\student.mdf;Integrated Security=True;User Instance=True");
                SqlCommand cmd4 = new SqlCommand("insert into " + tempclass + "track values ('" + userid + "','" + sub + "','" + date + "')", con4);
                con4.Open();

                cmd4.ExecuteNonQuery();
                con4.Close();

                //Response.Redirect("tracker.aspx");
            }
            else
            {
            }
        }
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Mohit Jain
  • 135
  • 1
  • 4
  • 12
  • Perhaps you should check the CheckBox `State` instead – MethodMan Apr 02 '13 at 19:32
  • How are you getting a user action with `Visible="False"`? – Candide Apr 02 '13 at 19:39
  • Candide . ignore visible = false..from 8 check boxes , I turn visible= true based on user on pageload event – Mohit Jain Apr 03 '13 at 17:05
  • At first glance the only place it appears you are changing the checked state is in Calendar1_SelectionChanged - just for testing purposes, why don't you comment out that body of that method and see if you are still encountering the same errors. – 99823 Apr 04 '13 at 12:36
  • thnx loren for going through the code.. I tried commenting but doesnot helps. I am concerned when the user unchecks checkbox1 why the else portion of checkchanged event is not getting executed – Mohit Jain Apr 04 '13 at 18:34

4 Answers4

5

From MSDN (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.oncheckedchanged.aspx):

A CheckBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.

When (html) checkbox is not checked and form is submitted, no value would be passed back to server, that's why you need to enable ViewState for this control, so ASP.NET would keep track of the state (checked/unchecked).

Floremin
  • 3,969
  • 15
  • 20
  • when i set enable viewstate= true, it doesnot allows me to uncheck the checkbox. on unchecking, page reloads and checkbox remains checked. – Mohit Jain Apr 03 '13 at 17:23
  • You probably have something else further in ASP.NET page lifecycle that is setting the checkbox after the event has been handled, or you are not reloading the data from the store after you have modified it in the event handler. – Floremin Apr 03 '13 at 17:30
  • thnx floremin.. but i am reloading the data ... i guess due to enableviewstate= true, the checkbox is retaining the previous state.. – Mohit Jain Apr 03 '13 at 17:39
  • some solution here.. but not understanding.. can anyone please elaborate ... http://stackoverflow.com/questions/1404528/oncheckedchanged-event-handler-of-aspcheckbox-does-not-fire-when-checkbox-is-un?rq=1 – Mohit Jain Apr 03 '13 at 17:48
  • Then you can set `CheckBox1.Checked = false`, after you handled INSERT or DELETE in your `CheckBox1_CheckedChanged` handler. – Floremin Apr 03 '13 at 17:56
  • Please go thru the entire code and show me which statement is not allowing the checkbox to get uncheck.. also in debugger mode, on uncheck, checkchanged is not executing at all. – Mohit Jain Apr 04 '13 at 04:34
4

Remove EnableViewState="false" and it should work fine

99823
  • 2,407
  • 7
  • 38
  • 60
  • 1
    Removing EnableViewState="false" doesnot help.. as when i check the checkbox, it gets checked and executes properly.. however it doesnot allow me to uncheck the checkbox. when i try to uncheck, page is reload and checkbox remains checked. – Mohit Jain Apr 03 '13 at 17:20
  • Are you checking this box from data coming out of a database? It sounds like there is something going on in your code behind file that is rechecking the box. – 99823 Apr 03 '13 at 19:26
  • I started a empty project, tested it and it works flawlessly every time, so I assume there is more code you aren't showing us that is affecting the checkboxes checked state. – 99823 Apr 03 '13 at 19:26
  • thnx loren.. considering your answer even i executed a new project and really it works... can you please go thru my entire code and answer where is the flaw in the code which is causing checkbox to remain check... P.S: this code is a part of attendance tracker application which displays timetable depending on user and lets him mark his own attendance – Mohit Jain Apr 04 '13 at 03:55
  • Please go thru the entire code and show me which statement is not allowing the checkbox to get uncheck.. also in debugger mode, on uncheck, checkchanged is not executing at all. – Mohit Jain Apr 04 '13 at 04:34
2

Thank you everyone for your consideration. Problem finally solved. On uncheck event by user the checkchanged method was not executing because 1)with autopostback on, uncheck caused execution of page_load method, which in turn caused the checkbox.visible= false, so checkchanged didnot executed, and then calendar render method caused checkbox.visible =true with the previous state(viewstate=true) ie checked state.

special thnx to floremin and loren.

Mohit Jain
  • 135
  • 1
  • 4
  • 12
  • I had the same problem. In my case the CheckBox was disabled by default (Enabled="false"). So ViewState wasn't loaded for the control. – algreat Feb 18 '14 at 21:01
1

Solution: you should set-> EnableViewstate = True, ViewStateMode = Enabled, Autopostback = true. that's all. this works me.

zuhal
  • 49
  • 1
  • 9