在asp.net中动态生成图像?

时间:2012-04-03 08:21:19

标签: c# asp.net dynamic panel

<li>

                                    <asp:Panel ID="Panel6" runat="server">
                                    <ul id="list2a" runat="server">

                                    </ul>

                                    </asp:Panel>
                                </li>
                                <li>
                                    <asp:Panel ID="Panel7" runat="server">

                                    <ul id="list2b" runat="server">
                                        <%--<li><a href="#">
                                            <img src="images/img.gif" width="80" height="80" alt="image description" />
                                            <span class="mask">&nbsp;</span>
                                        </a></li>
                                        <li><a href="#">
                                            <img src="images/img.gif" width="80" height="80" alt="image description" />
                                            <span class="mask">&nbsp;</span>
                                        </a></li>
                                        <li><a href="#">
                                            <img src="images/img.gif" width="80" height="80" alt="image description" />
                                            <span class="mask">&nbsp;</span>
                                        </a></li>--%>
                                    </ul>
                                    </asp:Panel>
                                </li>

我正在尝试在Panel内动态添加图像然后进入ul但是我需要在一个Panel中添加3个图像,在另一个Panel中添加下3个图像。

我的代码如下:

  string query2 = "SELECT [Pic_square] From [User1] WHERE ([FB_Id] IN (SELECT [Fb_Id2] FROM [Snapshot] WHERE [Fb_Id1]= '812534558' AND [Seeltr]='See Later')) UNION SELECT [Pic_square] From [Passive] WHERE ([FB_Id] IN (SELECT [Fb_Id2] FROM [Snapshot] WHERE [Fb_Id1]= '812534558' AND [Seeltr]='See Later'))";
        var adt = new SqlDataAdapter(query2, con);
        ds1 = new DataSet();
        adt.Fill(ds1);
        int count1 = ds1.Tables[0].Rows.Count;
        for (int j = 0; j < count1; j++)
        {
            HtmlGenericControl listitem1 = new HtmlGenericControl("li");
            HtmlGenericControl anchor1 = new HtmlGenericControl("a");
            Image im1 = new Image();
            im1.ImageUrl = (string)ds1.Tables[0].Rows[j].ItemArray[0];

            im1.Height = 80;
            im1.Width = 80;
            anchor1.Controls.Add(im1);
            //anchor1.Attributes.CssStyle.Add("div", "holder");
            listitem1.Controls.Add(anchor1);

            if(count1 <= 3)
           {
            list2a.Controls.Add(listitem1);
            Panel6.Controls.Add(list2a);
            }




            else if (3 < count1 && count1 <= 6)
            {


                list2b.Controls.Add(listitem1);
                Panel7.Controls.Add(list2b);
            }

 else if (6 < count1 && count1 <= 9)
            {
                //list2b.Controls.Add(listitem1);
                list2c.Controls.Add(listitem1);
                Panel8.Controls.Add(list2c);
            }
            else if (9 < count1 && count1 <= 12)
            {
                list2d.Controls.Add(listitem1);
                Panel9.Controls.Add(list2d);
               // list2b.Controls.Add(listitem1);
               // list2c.Controls.Add(listitem1);


            }
            else
            {
               // list2b.Controls.Add(listitem1);
                //list2c.Controls.Add(listitem1);
                //list2d.Controls.Add(listitem1);

                list2e.Controls.Add(listitem1);
                Panel10.Controls.Add(list2e);

            }

但我得到的只是6张照片,我不想要

编辑:PS我的面板最多10个,我添加了代码

请帮帮忙?

2 个答案:

答案 0 :(得分:2)

在代码listitem1.Controls.Add(anchor1);之后,将其替换为

if (j <= 2)
{
   //panel 1
}
if (j > 2 && j <= 5)
{
   //panel 2
}
if (j > 5 && j <= 8)
{
   //panel 3
}
and so no

答案 1 :(得分:0)

您只需添加if语句,它们总是被添加到上面的panel6中。

if (3 < count1 && count1 <= 6) {

     list2a.Controls.Add(listitem1)
     Panel6.Controls.Add(list2a);

}
else
{
      list2b.Controls.Add(listitem1);
      Panel7.Controls.Add(list2b);
}