samedi 27 mars 2021

Why ASP.NET show different result?

I programmed that if I click a checkbox the checkbox change color and surrounding checkboxes disappear. However, sometime it works or sometime it doesn't. this happens randomly. I used selected change event.

It always executes same code. why i get different result? What should i DO?

Sometimes the checkbox change color and surrounding checkboxes disappear. Sometime the checkbox simply checked and don't change color, and surrounding checkboxes don't disappear.

I program it to change table's bg color including the checkbox.

protected void CBs_CheckedChanged(object sender, EventArgs e)
{    
    int cur_x = 0;
    int cur_y = 0;

    int num_reserve = 0;

    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 28; j++)
        {
            if (cbs[i, j].Checked != diffmap[i, j])
            {
                //Response.Write(i + "_" + j);
                cur_x = i;
                cur_y = j;
                break;
            }
            }
        }

        if (txt_day1.Text != "")
            num_reserve = num_reserve + 1;
        if (txt_day2.Text != "")
            num_reserve = num_reserve + 1;
        if (txt_day3.Text != "")
            num_reserve = num_reserve + 1;
    
        int count_check = 0;
             
        for (int i = 0; i < 14; i++)
        {
            for (int j = 0; j < 28; j++)
            {
                if (cbs[i, j].Checked && cbs[i, j].Enabled)
                {
                    count_check = count_check + 1;
                }
            }
        }
           
        if (cbs[cur_x, cur_y].Checked)
        {
            if (cur_x < 4)
                temptd = (System.Web.UI.HtmlControls.HtmlTableCell)FindControl("m" + (cur_x + 1) + "_" + (cur_y + 1));
            else
                temptd = (System.Web.UI.HtmlControls.HtmlTableCell)FindControl("m" + (cur_x + 3) + "_" + (cur_y + 1));

            temptd.BgColor = "#CAB9F0";
            
            if ((num_reserve + count_check) >= 3)
            {
                for (int i = 0; i < 14; i++)
                {
                    for (int j = 0; j < 28; j++)
                    {
                        if (!cbs[i, j].Checked)
                        {
                            cbs[i, j].Visible = false;
                        }
                    }
                }
            }
            else
            {
                for (int k = 0; k < 14; k++)
                {
                    if (cur_y >= 27)
                    {
                        for (int l = cur_y - 1; l < cur_y + 1; l++)
                        {
                            if (!(k == cur_x && l == cur_y)) //a
                            {
                                cbs[k, l].Visible = false;
                            }
                        }
                    }
                    else
                    {
                        for (int l = cur_y - 1; l < cur_y + 2; l++)
                        {
                            if (!(k == cur_x && l == cur_y))  //b
                            {
                                cbs[k, l].Visible = false;
                            }
                        }
                    }
                }
            }
        }
        else
        {
            if (cur_x < 4)
                temptd = (System.Web.UI.HtmlControls.HtmlTableCell)FindControl("m" + (cur_x + 1) + "_" + (cur_y + 1));
            else
                temptd = (System.Web.UI.HtmlControls.HtmlTableCell)FindControl("m" + (cur_x + 3) + "_" + (cur_y + 1));

            if (cur_y != 0)
                temptd.BgColor = "#FFFFFF"; 
            else if (cur_y == 1)
            {
                string cur_time = DateTime.Now.ToString("HHmmss");
                int cur_time_int = int.Parse(cur_time.Substring(0, 4));
                if (cur_time_int >= 1800)
                    temptd.BgColor = "#DFDFDF";
                else
                    temptd.BgColor = "#FFFFFF";
            }
            else
                temptd.BgColor = "#DFDFDF";

            reservemap[cur_x, cur_y] = false;

            if ((num_reserve + count_check) >= 2)
            {
                for (int i = 0; i < 14; i++)
                {
                    for (int j = 0; j < 28; j++)
                    {
                        if (!cbs[i, j].Checked && !reservemap[i, j] && j != 0)
                        {
                            cbs[i, j].Visible = true;
                        }
                           
                        if (j == 1) 
                        {
                            string cur_time = DateTime.Now.ToString("HHmmss"); 
                            int cur_time_int = int.Parse(cur_time.Substring(0, 4));
                            if (cur_time_int >= 1800)
                            {
                                if (!cbs[i, j].Checked)
                                    cbs[i, j].Visible = false;
                            }
                        }
                    }
                }
                        
                for (int i = 0; i < 14; i++)
                {
                    for (int j = 0; j < 28; j++)
                    {
                        if (j > 0 && j < 28)
                        {
                            if (cbs[i, j - 1].Checked)
                            {
                                for (int k = 0; k < 14; k++)
                                    cbs[k, j].Visible = false;
                            }
                            if (j < 27)
                                if (cbs[i, j + 1].Checked)
                                {
                                    for (int k = 0; k < 14; k++)
                                        cbs[k, j].Visible = false;
                                }
                            }
                            if (cbs[i, j].Checked)
                            {
                                for (int k = 0; k < 14; k++)
                                    if (k != i)
                                        cbs[k, j].Visible = false;
                            }

                        }
                    }
                }
                else
                {                       
                    bool leftcheck = false;
                    bool rightcheck = false;

                    if (cur_y > 1)
                    {
                        for (int k = 0; k < 14; k++)
                        {
                            if (cbs[k, cur_y - 2].Checked)
                                leftcheck = true;
                        }
                    }

                    if (cur_y < 26)
                    {
                        for (int k = 0; k < 14; k++)
                        {
                            if (cbs[k, cur_y + 2].Checked)
                                rightcheck = true;
                        }
                    }

                    for (int k = 0; k < 14; k++)
                    {
                        if (cur_y >= 27)  //last
                        {
                            for (int l = cur_y - 1; l < cur_y + 1; l++)
                            {
                                if (!((l == (cur_y - 1)) && leftcheck))
                                {
                                    if (!(k == cur_x && l == cur_y))
                                    {
                                        if (!reservemap[k, l])
                                            cbs[k, l].Visible = true;
                                    }

                                }
                            }
                        }
                        else
                        {
                            for (int l = cur_y - 1; l < cur_y + 2; l++)
                            {
                                if (!((l == (cur_y - 1)) && leftcheck) && !((l == (cur_y + 1)) && rightcheck))
                                {
                                    if (!(k == cur_x && l == cur_y))
                                    {
                                        if (!reservemap[k, l])
                                            cbs[k, l].Visible = true;
                                    }
                                }
                            }
                        }
                    }
                }    
            }

            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j < 28; j++)
                {
                    diffmap[i, j] = cbs[i, j].Checked;
                }
            }    
        }



Aucun commentaire:

Enregistrer un commentaire