2013-12-13 73 views
1

我有一個GridView,它由我的數據庫中的圖像填充。我現在的GridView看起來像這樣:顯示儘可能多的列以適應GridView寬度ASP.NET

enter image description here

但我想適合我的網格寬度,像這樣:

enter image description here

這裏是我的代碼:

 <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="100%" GridLines="None"> 

<Columns> 

    <asp:ImageField DataImageUrlFormatString="http://myurl/{0}" DataImageUrlField="url" ControlStyle-Height="200px" ControlStyle-Width="200px"> 
<ControlStyle Height="200px" Width="200px"></ControlStyle> 
    </asp:ImageField> 

</Columns> 
</asp:GridView> 

任何幫助appriciated。

回答

2

您可以在此情況下,具有3列

它的工作原理大致相同的GridView改用GridView控件的數據列表。

<asp:DataList ID="dlImages" runat="server" RepeatColumns="4" CellPadding="3" CellSpacing="3" 
             RepeatDirection="Horizontal"> 
             <ItemTemplate> 
              <table class="TableBorder"> 
               <tr> 
                <td class="NormalTextBig" valign="top" align="left"> 
                 <%#Container.ItemIndex + 1%>. 
                </td> 
               </tr> 
               <tr> 
                <td valign="top" align="left"> 
                 <asp:ImageButton ID="ImageButton1" runat="server" CommandName="ImageClick" CommandArgument='<%# Eval("PageName") %>' /> 
                </td> 
               </tr> 
              </table> 
             </ItemTemplate> 
            </asp:DataList> 

綁定:

Protected Sub dlImages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlImages.ItemDataBound 
      If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then 

       CType(e.Item.FindControl("ImageButton1"), ImageButton).ImageUrl = "ImageResize.aspx?ImageName=ScreenMasterImages/" & e.Item.DataItem("ImgName") & "&BoxSize=" & 300 
       CType(e.Item.FindControl("ImageButton1"), ImageButton).ToolTip = e.Item.DataItem("PageName") 
      End If 
     End Sub 

就像你在我的項目中提到我已經使用了相同的目的,該代碼。

ItemCommand:

Protected Sub dlImages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlImages.ItemCommand 
      If e.CommandName = "ImageClick" Then 
       Session("ImageName") = e.CommandArgument 
       Response.Redirect("ScreenDetails.aspx") 
      End If 
     End Sub 

這是怎麼會看:

enter image description here

我想你也有類似的事情:)

相關問題