📄 gridval.aspx.cs
字号:
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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -