gridval.aspx.cs

来自「关于ASP.NET制作程序模块」· CS 代码 · 共 82 行

CS
82
字号
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 + =
减小字号Ctrl + -
显示快捷键?