上一例中BoundColumn 控件不是唯一可以在 DataGrid 的 Columns 集合中设置的控件。还可以指定 TemplateColumn,它使您可以完全控制列的内容。模板的内容可以是任意的;在 DataGrid 的列中可以呈现任何内容,包括服务器控件。下面的示例说明如何使用 TemplateColumn 控件将“State”列呈现为下拉列表并将“Contract”列呈现为复选框 HtmlControl。ASP.NET 数据绑定语法用于输出模板中的数据字段值。注意,有一些棘手的逻辑使下拉列表和复选框反射行中的数据状态。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
SqlConnection myConnection;
public Hashtable StateIndex;
protected void Page_Load(Object Src, EventArgs E)
{
myConnection = new SqlConnection("user id=sa;password=;initial
catalog=pubs;data source=jeff");
if (!IsPostBack)
BindGrid();
StateIndex = new Hashtable();
StateIndex["CA"] = 0;
StateIndex["IN"] = 1;
StateIndex["KS"] = 2;
StateIndex["MD"] = 3;
StateIndex["MI"] = 4;
StateIndex["OR"] = 5;
StateIndex["TN"] = 6;
StateIndex["UT"] = 7;
}
public int GetStateIndex(String stateName)
{
if (StateIndex[stateName] != null)
return (int)StateIndex[stateName];
else
return 0;
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{
String updateCmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
+ "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",
SqlDbType.NVarChar, 11));
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
String[] cols = {"LName","FName","Phone","Address","City","Zip"};
for (int i=0; i<6; i++)
{
String colvalue = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text;
// 检查在所需字段中是否有空值
if (i<3 && colvalue == "")
{
Message.InnerHtml = "错误:“姓名”或“电话”不允许使用空值";
Message.Style["color"] = "red";
return;
}
myCommand.Parameters["@" + cols[i]].Value = colvalue;
}
myCommand.Parameters["@State"].Value = ((DropDownList)E.Item.FindControl("edit_State")).SelectedItem.ToString();
if (((CheckBox)E.Item.FindControl("edit_Contract")).Checked == true)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0";
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
myCommand.Connection.Close();
BindGrid();
}
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from
Authors", myConnection);
DataSet ds = new DataSet();