需要知道如何使代码更好

时间:2016-08-21 06:49:31

标签: asp.net

           variable = reader.GetInt32(reader.GetOrdinal("seatno"));

                        if (variable == 1)
                        {
                            ImageButton img1 =    (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton4");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;



                        }
                        if (variable == 2)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton6");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 3)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton7");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 4)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton8");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 5)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton9");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }

                        if (variable == 6)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton10");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 7)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton11");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 8)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton12");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 9)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton13");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 10)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton14");
                            //  ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 11)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton15");
                            //  ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 12)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton20");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 13)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton21");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 14)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton22");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 15)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton23");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 16)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton24");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 17)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton25");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 18)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton26");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 19)
                        {
                            ImageButton img1 = (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton27");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 20)
                        {
                            ImageButton img1 =     (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton15");

                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }
                        if (variable == 21)
                        {
                            ImageButton img1 =      (ImageButton)GridView2.Rows[0].Cells[0].FindControl("ImageButton26");
                            //  ImageButton img2 = (ImageButton)img1.FindControl("ImageButton4");
                            img1.ImageUrl = "~/images/saveseat.jpg";
                            img1.Enabled = false;
                        }

请告诉我如何使这段代码变得更好和更短。我必须经历每一个条件。这是耗时的。我需要这样做,如果变量值是4,它必须直接去4而不是经历每一个if。

        if (image4click)
        {
            a = 1;

        }
        if (image4click1)
        {
            a = 2;
        }
        if (image4click2)
        {
            a = 3;
        }
        if (image4click3)
        {
            a = 4;
        }
        if (image4click4)
        {
            a = 5;
        }
        if (image4click5)
        {
            a = 6;
        }
  in this image4click is boolean variable.how to shorten this make it    better.

1 个答案:

答案 0 :(得分:0)

我不确定您使用的是哪种语言,但在编程中有一种叫做switch-case的东西。

    switch 
      case 1:
         do this...
      case 2: 
         do this...
      case 3
         do this...
    end

显然,您必须查看您正在使用的编程语言的语法。希望这有帮助!