ASP.NET 强类型GridView
示例
从Asp.net4.5Web控件开始,可以利用强类型绑定来获得IntelliSense支持和编译时错误。
创建一个包含模型的类:
public class Album
{
public int Id { get; set; }
public string Name { get; set; }
public string Artist { get; set; }
}在页面上定义GridView控件:
<asp:GridView ID="Grid" runat="server" AutoGenerateColumns="false" ItemType="YourNamespace.Album">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text="<%#Item.Id%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text="<%#Item.Name%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Artist">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text="<%#Item.Artist%>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>加载数据并绑定它:
var albumList = new List<Album>
{
new Album {Id = 1, Artist = "Icing (a Cake cover band)", Name = "Toppings Vol. 1"},
new Album {Id = 2, Artist = "Fleetwood PC", Name = "Best of Windows"},
new Album {Id = 3, Artist = "this.Bandnames", Name = "TH_ (Pronounced \"Thunderscore\")"},
};
Grid.DataSource = albumList;
Grid.DataBind();
热门推荐
10 退休前辈文案祝福语简短
11 边防女兵祝福语大全简短
12 作文结尾新年祝福语简短
13 男人提新车祝福语简短
14 情侣签名简短英文祝福语
15 女生迟到文案祝福语简短
16 好友搞笑留言祝福语简短
17 妹妹结婚伴娘祝福语简短
18 祝福语老师诗词朗诵简短