需要知道如何使代码更
问题描述:
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.
答
我不确定你正在使用哪种语言,但在编程中有一些称为switch-case的东西。
switch
case 1:
do this...
case 2:
do this...
case 3
do this...
end
很明显,你将不得不看看你正在使用的编程语言的语法。希望这可以帮助!
非常感谢.. :) :) – nimmy
不客气:) –