] 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();
myCommand.Fill(ds, "Authors");
MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
}
</script>
<body style="font: 10.5pt 宋体">
<form runat="server">
<h3><font face="宋体">更新具有模板列的数据行</font></h3>
<span id="Message" EnableViewState="false" style="font: arial 11pt;"
runat="server"/><p>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="