2

我正在尝试使用 C# 将多个图像添加到 ASP.net 中数据网格视图的列中

图像名称在数据库中,一些用户将有 10 个图像,而其他用户将没有图像,而有些则介于两者之间。

我发现我可以添加一个 ItemTemplate 但我想要的是在加载时以编程方式生成数字,

这可能吗

 <asp:GridView ID="dgvTopPlayers" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="dgvTopPlayers_RowDataBound" 
        onrowcreated="dgvTopPlayers_RowCreated" ShowHeaderWhenEmpty="True">
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="UserID" 
                DataNavigateUrlFormatString="~/UserProfile.aspx?UserId={0}" 
                DataTextField="UserName" HeaderText="UserName" 
                />
            <asp:BoundField DataField="Last7Days" HeaderText="Last7Days" />
            <asp:BoundField DataField="LastMonth" HeaderText="LastMonth" />
            <asp:BoundField DataField="TotalPoints" HeaderText="TotalPoints" />
            <asp:BoundField DataField="IdeaCount" HeaderText="IdeaCount" />
            <asp:BoundField DataField="ChallengeCount" HeaderText="ChallengeCount" />
            <asp:TemplateField HeaderText="Badges"  >
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("BadgeIcons") %>' 
                        Tooltip='<%# Eval("BadgeIcons") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" AlternateText='<%# Eval("BadgeIcons") %>' 
                        ImageUrl='<%# Eval("BadgeIcons") %>' />
                        <asp:Image ID="Image2" runat="server" AlternateText='<%# Eval("BadgeIcons") %>' 
                        ImageUrl="~/Images/puzzleIcon.png" />
                        <asp:Image ID="Image3" runat="server" AlternateText='<%# Eval("BadgeIcons") %>' 
                        ImageUrl="~/IMAGES/caution.png" />
                        <asp:Image ID="Image4" runat="server" AlternateText='<%# Eval("BadgeIcons") %>' 
                        ImageUrl="~/IMAGES/reportabuse.png" />
                        <asp:Image ID="Image5" runat="server" AlternateText='<%# Eval("BadgeIcons") %>' 
                        ImageUrl="~/IMAGES/yellow_star.png" />
                </ItemTemplate>


                <ItemStyle Width="35%" />


            </asp:TemplateField>
        </Columns>

    </asp:GridView>
4

1 回答 1

1

在 内部放置一个面板或占位符ItemTemplate,并在 RowDataBound 中添加图像,因为您已经拥有它。

<asp:TemplateField HeaderText="Badges"  >
   <EditItemTemplate>
      <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("BadgeIcons") %>' 
         Tooltip='<%# Eval("BadgeIcons") %>'></asp:TextBox>
   </EditItemTemplate>
   <ItemTemplate>
      <asp:Panel ID="ImagePanel".../>
  </ItemTemplate>
</asp:TemplateField>


void dgvTopPlayers_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   if(e.Row.RowType == DataControlRowType.DataRow)
   {
      // *** Cast to appropiate object ***
      var user = e.Row.DataItem as User;
      var panel = e.Row.FindControl("ImagePanel") as Panel;
      var images = user.BadgeIcons.Split(',');      
      foreach(var img in images) {
         panel.Controls.Add(new Image{ ID = UniqueID, ImageUrl = "~/IMAGES/" + img });
      }
   }
}  
于 2013-03-20T17:00:39.880 回答