2016-08-21 136 views
-2
  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; 
         } 

請告訴我怎樣可以使這個代碼更優秀,short.here我必須去通過每個如果condition.which是時候consuming.i需要使它這樣,如果變量值是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. 

回答

0

我不確定你正在使用哪種語言,但在編程中有一些稱爲switch-case的東西。

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

很明顯,你將不得不看看你正在使用的編程語言的語法。希望這可以幫助!

+0

非常感謝.. :) :) – nimmy

+0

不客氣:) –