Difference between revisions of "GridView"
Line 1: | Line 1: | ||
− | Das Sample, das in jedem Buch fehlt. | + | Das Sample, das in jedem Buch fehlt: |
+ | |||
+ | * Templating ohne die hässliche Eval funktion. | ||
+ | * funktionierendes Paging | ||
+ | |||
<source lang="csharp"> | <source lang="csharp"> | ||
using System; | using System; |
Revision as of 18:31, 11 November 2009
Das Sample, das in jedem Buch fehlt:
- Templating ohne die hässliche Eval funktion.
- funktionierendes Paging
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = this.CreateTable();
this.GridView1.DataBind();
}
public void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView) e.Row.DataItem;
bool value = (bool) row["BooleanColumn"];
Label myLabel = (Label) e.Row.FindControl("GridViewLabel");
if (value)
{
myLabel.Text = "Ja";
}
else
{
myLabel.Text = "Nein";
}
}
}
protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}
private DataTable CreateTable()
{
DataTable table = new DataTable();
DataColumn column;
DataRow row;
//First Column
column = new DataColumn();
column.DataType = typeof(Int32);
column.ColumnName = "id";
column.Unique = true;
table.Columns.Add(column);
//Second Column
column = new DataColumn();
column.DataType = typeof(bool);
column.ColumnName = "BooleanColumn";
table.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
for (int i = 0; i <= 200; i++)
{
row = table.NewRow();
row["id"] = i;
row["BooleanColumn"] = i%2 == 0 ? true : false;
table.Rows.Add(row);
}
return table;
}
}
und die Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
onrowdatabound="RowDataBound"
AllowPaging="true"
PageSize="10"
PageIndex="1"
OnPageIndexChanging="PageIndexChanging">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="The ominous id" DataField="id"/>
<asp:TemplateField HeaderText="Ansehen" >
<ItemTemplate>
<asp:Label ID="GridViewLabel" runat="server" Text="Label"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>