如何隐藏图像控制器?

时间:2015-05-23 10:12:15

标签: c# asp.net image

我的页面中有五个图像控制器。我想隐藏图像控制器,如果他们没有任何图像。现在我正如下面给出的那样。

enter image description here

代码:

DataTable dt = new DataTable();
dt = tt.GetImage(Ids);
foreach (DataRow row in dt.Rows)
 {
   byte[] img1 = (byte[])row["Im1"];
   string base1 = Convert.ToBase64String(img1);
   Image1.ImageUrl = "data:image/jpg;base64," + base1;

   byte[] img2 = (byte[])row["Im2"];
   string base2 = Convert.ToBase64String(img2 );
   Image2.ImageUrl = "data:image/jpg;base64," + base2 ;

   byte[] img3 = (byte[])row["Im3"];
   string base3 = Convert.ToBase64String(img3 );
   Image3.ImageUrl = "data:image/jpg;base64," + base3 ;

   byte[] img4 = (byte[])row["Im4"];
   string base4 = Convert.ToBase64String(img4 );
   Image4.ImageUrl = "data:image/jpg;base64," + base4 ;

   byte[] img5 = (byte[])row["Im5"];
   string base5 = Convert.ToBase64String(img5 );
   Image5.ImageUrl = "data:image/jpg;base64," + base5 ;
 }

ASPX:

<asp:Image ID="Image1" runat="server" Width="559px" Height="210px"/>
<br />
<asp:Image ID="Image2" runat="server" Width="559px" Height="210px"/>
<br />
<asp:Image ID="Image3" runat="server" Width="559px" Height="210px"/>
<br />
<asp:Image ID="Image4" runat="server" Width="559px" Height="210px"/>
<br />
<asp:Image ID="Image5" runat="server" Width="559px" Height="210px"/>

1 个答案:

答案 0 :(得分:2)

看看这对你有帮助,我没有测试过。

        DataTable dt = new DataTable();
        dt = tt.GetImage(Ids);

        foreach (DataRow row in dt.Rows)
        {
            byte[] img1 = (byte[])row["Im1"];
            string base1 = Convert.ToBase64String(img1);
            Image1.ImageUrl = "data:image/jpg;base64," + img1;
            Image1.Visible = (!string.IsNullOrEmpty(base1));

            byte[] img2 = (byte[])row["Im2"];
            string base2 = Convert.ToBase64String(img2);
            Image2.ImageUrl = "data:image/jpg;base64," + base2;
            Image2.Visible = (!string.IsNullOrEmpty(base2));

            byte[] img3 = (byte[])row["Im3"];
            string base3 = Convert.ToBase64String(img3);
            Image3.ImageUrl = "data:image/jpg;base64," + base3;
            Image3.Visible = (!string.IsNullOrEmpty(base3));

            byte[] img4 = (byte[])row["Im4"];
            string base4 = Convert.ToBase64String(img4);
            Image4.ImageUrl = "data:image/jpg;base64," + base4;
            Image4.Visible = (!string.IsNullOrEmpty(base4));

            byte[] img5 = (byte[])row["Im5"];
            string base5 = Convert.ToBase64String(img5);
            Image5.ImageUrl = "data:image/jpg;base64," + base5;
            Image5.Visible = (!string.IsNullOrEmpty(base5));
        }