动态指定要在中继器中显示的图像&的DataList

时间:2017-01-05 20:35:46

标签: c# asp.net sql-server

我在SQL Server数据库中有一个Group表和一个Images表:

组表:( Group_Id连接到groupNo,下方)

sql1

图片表:

sql 2

下面的代码显示“组”表中每行的DataList,但不会将图像分隔到相关的组中。相反,它显示每行中的所有图像:

 <table>
                                <tr>
                                    <td colspan="2">
                                        <asp:Repeater ID="rptGroupGallery" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                                            <ItemTemplate>
                                                <asp:DataList ID="dlImages" runat="server" DataKeyField="ID" RepeatDirection="Horizontal" RepeatColumns="3" CellPadding="5">
                                                    <ItemTemplate>
                                                        <div style="padding-bottom: 10px; padding-right: 10px">
                                                            <a id="imageLink" href="~/SlideImages/<%# Eval("filename") %>" title="<%#Eval("imageDesc") %>" rel="lightbox[Brussels]">
                                                                <asp:Image ID="Image1" ImageUrl='<%# "~/SlideImages/" + Eval("filename") %>' runat="server" Width="112" Height="84" />
                                                            </a>
                                                        </div>
                                                    </ItemTemplate>
                                                </asp:DataList>
                                            </ItemTemplate>
                                        </asp:Repeater>

                                    </td>
                                </tr>
                            </table>

这是我的C#:

 static String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["BallinoraDBConnectionString1"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //find the nested datalist and cast it as one
    DataList datalist = e.Item.FindControl("dlImages") as DataList;

    //find the correct group id of the item
    string Group_Id = DataBinder.Eval(e.Item.DataItem, "Group_Id").ToString();
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Groups WHERE (Group_Id = '" + Group_Id +  "')", con);
    DataTable dt = new DataTable();
    sda.Fill(dt);

    //bind data to the nested datalist with the Group_Id in the where clause of the query
    datalist.DataSource = dt;
    datalist.DataBind();
}


protected void Page_Load(object sender, EventArgs e)
{
    //Trying to display events using repeater
    SqlConnection connR;
    string connectionStringR = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    connR = new SqlConnection(connectionStringR);
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Groups", connR);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    //rptGroupGallery.DataSource = dt;
    Repeater1.DataSource = dt;
    //rptGroupGallery.DataBind();
    Repeater1.DataBind();
}

我想为Groups表中的每一行显示一个DataList。此DataList将显示Images表中groupNo = Group_Id。

的图像

我尝试用Repeater包围我的代码,但我无法理解。

1 个答案:

答案 0 :(得分:1)

正如@ sh1rts已经提到的,你可以使用像Repeater这样的东西并嵌套DataList。然后,您需要向Repeater添加OnItemDataBound事件,以使用正确的数据填充每个项目中的DataList。

<table>
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <tr>
                <td><%# Eval("Title") %></td>
                <td>
                    <asp:DataList ID="dlImages" runat="server" DataKeyField="ID" RepeatDirecton="Horizontal" RepeatColumns="3" CellPadding="5">
                        <ItemTemplate>
                            <a id="imageLink" href="~/SlideImages/<%# Eval("filename") %>" title="<%# Eval("imageDesc") %>" rel="lightbox[Brussels]">
                                <asp:Image ID="Image1" ImageUrl='<%# "~/SlideImages/" + Eval("filename") %>' runat="server" Width="112" Height="84" />
                            </a>
                        </ItemTemplate>
                    </asp:DataList>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

背后的代码

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //find the nested datalist and cast it as one
    DataList datalist = e.Item.FindControl("dlImages") as DataList;

    //find the correct group id of the item
    string Group_Id = DataBinder.Eval(e.Item.DataItem, "Group_Id").ToString();

    //bind data to the nested datalist with the Group_Id in the where clause of the query
    datalist.DataSource = dt;
    datalist.DataBind();
}