前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridval.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
<style type="text/css">
.td
{
border-bottom:dotted 1px #dcdcdc;
border-right:dotted 1px #dcdcdc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="border:solid 1px green;width:40%">
<tr>
<td style="background-color:Green"><strong><span style="font-size: 14pt; color: #ffffff">验证GridView控件中编辑列的值</span></strong>
</td>
</tr>
<tr>
<td>
<asp:GridView Width="100%" ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductName" ReadOnly="True" HeaderText="商品名称"/>
<asp:TemplateField>
<HeaderTemplate>
<table width="100%">
<tr>
<td align="left" width="60%" >数量</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td width="20%"><%# Eval("UnitsInStock")%></td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td width="20%"><asp:TextBox runat="Server" ID="t1" Text='<%# Eval("UnitsInStock")%>' Width="90%"></asp:TextBox></td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="操作" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
codebehind:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
band();
}
}
private void band()
{
SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["val"].ConnectionString);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select top 5 ProductID, ProductName, UnitsInStock from products", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
band();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
band();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["val"].ConnectionString);
con.Open();
string aa = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string bb = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("t1")).Text.ToString();
string cc = "update products set UnitsInStock='" + bb + "' where ProductID='" + aa +"'";
SqlCommand cmd = new SqlCommand(cc, con);
cmd.ExecuteNonQuery();
con.Close();
this.GridView1.EditIndex = -1;
band();
Response.Write("<script>alert('更新成功!')</script>");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit)==DataControlRowState.Edit)
{
RequiredFieldValidator re = new RequiredFieldValidator();
re.ControlToValidate = "t1";
re.ErrorMessage = "销售量不能为空";
re.Display = ValidatorDisplay.Dynamic;
re.Text = "*";
e.Row.Cells[1].Controls.Add(re);
RangeValidator ra = new RangeValidator();
ra.ControlToValidate = "t1";
ra.Type = ValidationDataType.Integer;
ra.Display = ValidatorDisplay.Dynamic;
ra.MaximumValue = "200";
ra.MinimumValue = "10";
ra.Text = "*";
ra.ErrorMessage = "销售量的范围为10-200";
e.Row.Cells[1].Controls.Add(ra);
ValidationSummary va = new ValidationSummary();
va.ShowSummary = false;
va.ShowMessageBox = true;
e.Row.Cells[1].Controls.Add(va);
}
}
}