ASP.NET DEMO 17:GridViewバインド列/テンプレート列の動的作成
12187 ワード
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%--http://topic.csdn.net/u/20080523/01/bab3a401-ab6d-4a9f-b946-aa04ea132442.html--%>
<%--ASP.NET 2.0 GridView http://dotnet.aspx.cc/article/897cb049-ccff-4fe7-b990-df974493a0b1/read.aspx ( )--%>
<script runat="server">
GridView grdStudent;
public enum StudentGridViewHeader
{
StudentNO,
Name,
Age,
Gender
}
public class StudentGridViewTemplate : ITemplate
{
private DataControlRowType rowType;
private DataControlRowState rowState;
private StudentGridViewHeader header;
public StudentGridViewTemplate(StudentGridViewHeader header, DataControlRowType rowType)
: this(header, rowType, DataControlRowState.Normal)
{
}
public StudentGridViewTemplate(StudentGridViewHeader header, DataControlRowType rowType, DataControlRowState rowState)
{
this.header = header;
this.rowType = rowType;
this.rowState = rowState; // ,
}
public void InstantiateIn(Control container)
{
switch (rowType)
{
case DataControlRowType.Header:
Literal ltlHeader = new Literal();
ltlHeader.Text = header.ToString();
container.Controls.Add(ltlHeader);
break;
case DataControlRowType.DataRow:
switch (header)
{
case StudentGridViewHeader.StudentNO:
HyperLink lnkStudentNO = new HyperLink();
lnkStudentNO.DataBinding += delegate(object sender, EventArgs e) //
{
object dataItem = ((sender as Control).NamingContainer as GridViewRow).DataItem;
lnkStudentNO.Text = (dataItem as DataRowView)["StudentNO"].ToString(); // DataSet/DataTable/DataView , ,
lnkStudentNO.NavigateUrl = DataBinder.Eval(dataItem, "StudentNO", "studentdetail.aspx?sno={0}"); // , ,
};
container.Controls.Add(lnkStudentNO);
break;
case StudentGridViewHeader.Name:
Literal ltlName = new Literal();
ltlName.DataBinding += delegate(object sender, EventArgs e)
{
object dataItem = ((sender as Control).NamingContainer as GridViewRow).DataItem;
ltlName.Text = DataBinder.Eval(dataItem, "LastName") + " " + DataBinder.Eval(dataItem, "FirstName");
};
container.Controls.Add(ltlName);
break;
case StudentGridViewHeader.Gender:
DropDownList ddlGender = new DropDownList();
ddlGender.Items.AddRange(new ListItem[] { new ListItem("Male", "M"), new ListItem("Female", "F") });
ddlGender.DataBound += delegate(object sender, EventArgs e)
{
object dataItem = ((sender as Control).NamingContainer as GridViewRow).DataItem;
ddlGender.SelectedValue = DataBinder.Eval(dataItem, "Gender").ToString(); //
};
container.Controls.Add(ddlGender);
break;
default:
throw new InvalidOperationException("NotSupported StudentGridViewHeader: " + header.ToString());
}
break;
default:
throw new InvalidOperationException("NotSupported GridViewRowType: " + rowType.ToString());
}
}
}
void CreateStudentGridView()
{
//
grdStudent = new GridView();
grdStudent.AutoGenerateColumns = false;
grdStudent.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
{
};
//
TemplateField tplField = new TemplateField();
tplField.ShowHeader = true;
tplField.HeaderTemplate = new StudentGridViewTemplate(StudentGridViewHeader.StudentNO, DataControlRowType.Header, DataControlRowState.Normal);
tplField.ItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.StudentNO, DataControlRowType.DataRow, DataControlRowState.Normal);
tplField.AlternatingItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.StudentNO, DataControlRowType.DataRow, DataControlRowState.Alternate);
grdStudent.Columns.Add(tplField);
//
tplField = new TemplateField();
tplField.ShowHeader = true;
tplField.HeaderTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Name, DataControlRowType.Header, DataControlRowState.Normal);
tplField.ItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Name, DataControlRowType.DataRow, DataControlRowState.Normal);
tplField.AlternatingItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Name, DataControlRowType.DataRow);
tplField.EditItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Name, DataControlRowType.DataRow, DataControlRowState.Edit);
grdStudent.Columns.Add(tplField);
//
BoundField bndField = new BoundField();
bndField.HeaderText = StudentGridViewHeader.Age.ToString();
bndField.DataField = StudentGridViewHeader.Age.ToString();
grdStudent.Columns.Add(bndField);
//
tplField = new TemplateField();
tplField.ShowHeader = true;
tplField.HeaderTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Gender, DataControlRowType.Header, DataControlRowState.Normal);
tplField.ItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Gender, DataControlRowType.DataRow, DataControlRowState.Normal);
tplField.AlternatingItemTemplate = new StudentGridViewTemplate(StudentGridViewHeader.Gender, DataControlRowType.DataRow, DataControlRowState.Alternate);
grdStudent.Columns.Add(tplField);
//
PlaceHolder1.Controls.Add(grdStudent);
}
void ShowStudentData()
{
if (grdStudent == null) throw new InvalidOperationException("The StudentGridView has not been created yet.");
DataTable dt = CreateSampleData();
grdStudent.DataSource = dt;
grdStudent.DataBind();
}
#region sample data
static DataTable CreateSampleEmptyDataTable()
{
DataTable tbl = new DataTable("Student");
tbl.Columns.Add("StudentNO", typeof(string));
tbl.Columns.Add("FirstName", typeof(string));
tbl.Columns.Add("LastName", typeof(string));
tbl.Columns.Add("Age", typeof(int));
tbl.Columns.Add("Gender", typeof(string));
return tbl;
}
static DataTable CreateSampleData()
{
DataTable tbl = CreateSampleEmptyDataTable();
tbl.Rows.Add("20021342", "Jack", "Wu", 25, "M");
tbl.Rows.Add("20025341", "Jue", "You", 23, "F");
tbl.Rows.Add("20022254", "Viky", "Huang", 24, "F");
tbl.Rows.Add("20022231", "Leo", "Wong", 24, "M");
return tbl;
}
#endregion
protected void Page_Init(object sender, EventArgs e)
{
// GridView
CreateStudentGridView();
}
protected void Page_Load(object sender, EventArgs e)
{
// , ViewState
if (!IsPostBack)
{
ShowStudentData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DEMO17_GridViewDynamicalCreateOnFly</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>
GridView </h3>
<ul>
<li> GridView , , , Template ASP.NET 。
</li>
<li> , / , , Button_Click , PostBack , 。
</li>
<li> : as (C# )</li>
<li> , , , ASP.NET ViewState , 。
<br>
:</li>
<ul>
<li> DataSource DataTable/DataView( DTO), Column, GridView.AutoGenerateColumns=true, GridView 。
</li>
<li> Column, GridView.Columns[index].Visible 。</li>
</ul>
</ul>
<br />
<input id="Button2" type="button" value="Redirect" onclick="location.href=location.href;" />
<asp:Button ID="Button1" runat="server" Text="PostBack" OnClick="Button1_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>