📄 editcustomer.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SellManageJane
{
/// <summary>
/// Editcustomer 的摘要说明。
/// </summary>
public class Editcustomer : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.WebControls.DataGrid DataGridCustomer;
protected System.Web.UI.WebControls.DropDownList Dropdownlist1;
protected System.Web.UI.WebControls.TextBox Txtkey;
protected System.Web.UI.WebControls.Button BtnFound;
SqlConnection conn;
SqlDataAdapter ada;
SqlCommand cmd;
protected System.Web.UI.HtmlControls.HtmlInputText DatePicker1;
DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
conn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"].ToString());
ds = new DataSet();
if(!Page.IsPostBack)
{
BindGrid();
Dropdownlist1.Items.Insert(0,new ListItem("任意条件","*"));
}
}
private void BindGrid()
{
ada = new SqlDataAdapter("select * from Customer",conn);
ada.Fill(ds,"Customer");
this.DataGridCustomer.DataSource=ds.Tables["Customer"].DefaultView;
this.DataGridCustomer.DataKeyField = "Id";
this.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnFound.Click += new System.EventHandler(this.BtnFound_Click);
this.DataGridCustomer.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridCustomer_PageIndexChanged_1);
this.DataGridCustomer.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridCustomer_CancelCommand_1);
this.DataGridCustomer.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridCustomer_EditCommand_1);
this.DataGridCustomer.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridCustomer_UpdateCommand_1);
this.DataGridCustomer.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridCustomer_DeleteCommand_1);
this.DataGridCustomer.SelectedIndexChanged += new System.EventHandler(this.DataGridCustomer_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BtnFound_Click(object sender, System.EventArgs e)
{
try
{
if(Dropdownlist1.SelectedValue=="客户姓名")
{
ada = new SqlDataAdapter("select * from Customer where customerName like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"CustomeruserName");
if(ds.Tables["CustomeruserName"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["CustomeruserName"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(Dropdownlist1.SelectedValue=="客户编号")
{
ada = new SqlDataAdapter("select * from Customer where customerId like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"CustomercustomerId");
if(ds.Tables["CustomercustomerId"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["CustomercustomerId"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(Dropdownlist1.SelectedValue=="城市")
{
ada = new SqlDataAdapter("select * from Customer where city like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"Customercity");
if(ds.Tables["Customercity"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["Customercity"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(Dropdownlist1.SelectedValue=="录入时间")
{
ada = new SqlDataAdapter("select * from Customer where visitedTime like '%"+ DatePicker1.Value +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"CustomervisitedTime");
if(ds.Tables["CustomervisitedTime"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["CustomervisitedTime"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(Dropdownlist1.SelectedValue=="*")
{
ada = new SqlDataAdapter("select * from Customer",conn);
ds = new DataSet();
ada.Fill(ds,"Customer1");
if(ds.Tables["Customer1"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["Customer1"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else
{
ada = new SqlDataAdapter("select * from Customer",conn);
ds = new DataSet();
ada.Fill(ds,"Customer");
if(ds.Tables["Customer"].Rows.Count!=0)
{
this.DataGridCustomer.DataSource = ds.Tables["Customer"].DefaultView;
this.DataGridCustomer.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
private void DataGridCustomer_CancelCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGridCustomer.EditItemIndex=-1;
BindGrid();
}
private void DataGridCustomer_DeleteCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
cmd = new SqlCommand("delete from Customer where id=@id",conn);
cmd.Parameters.Add("@id",SqlDbType.Int);
cmd.Parameters["@id"].Value = int.Parse(this.DataGridCustomer.DataKeys[e.Item.ItemIndex].ToString());
cmd.Connection.Open();
if(cmd.ExecuteNonQuery()>0)
{
RegisterStartupScript("errmsg","<script>alert('删除成功!');</script>");
cmd.Connection.Close();
}
else
{
RegisterStartupScript("errmsg","<script>alert('删除失败!');</script>");
}
this.BindGrid();
}
catch(Exception ex)
{
//Response.Write(ex.Message);
string msg = ex.Message.ToString();
RegisterStartupScript("errmsg","<script>alert('删除失败,DELETE 语句与 COLUMN REFERENCE 约束 FK__Sale__customerId__023D5A04 冲突。该冲突发生于数据库 SellManage,表 Sale, column customerId。语句已终止。');</script>");
}
}
private void DataGridCustomer_PageIndexChanged_1(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGridCustomer.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void DataGridCustomer_UpdateCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
cmd = new SqlCommand("update Customer set customerId=@customerId,customerName=@customerName,visitedTime=@visitedTime,"+
"city=@city,telephone=@telephone,address=@address,department=@department,remark=@remark where Id=@Id",conn);
cmd.Parameters.Add("@id",SqlDbType.Int);
cmd.Parameters.Add("@customerId",SqlDbType.VarChar,30);
cmd.Parameters.Add("@customerName",SqlDbType.VarChar,50);
cmd.Parameters.Add("@visitedTime",SqlDbType.DateTime);
cmd.Parameters.Add("@city",SqlDbType.VarChar,20);
cmd.Parameters.Add("@telephone",SqlDbType.VarChar,20);
cmd.Parameters.Add("@address",SqlDbType.VarChar,255);
cmd.Parameters.Add("@department",SqlDbType.VarChar,255);
cmd.Parameters.Add("@remark",SqlDbType.VarChar,255);
cmd.Parameters["@id"].Value = int.Parse(this.DataGridCustomer.DataKeys[e.Item.ItemIndex].ToString());
cmd.Parameters["@customerId"].Value =((TextBox)e.Item.Cells[2].Controls[0]).Text ;
cmd.Parameters["@customerName"].Value =((TextBox)e.Item.Cells[3].Controls[0]).Text;
cmd.Parameters["@visitedTime"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
cmd.Parameters["@city"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
cmd.Parameters["@telephone"].Value = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
cmd.Parameters["@address"].Value = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
cmd.Parameters["@department"].Value = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
cmd.Parameters["@remark"].Value = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
cmd.Connection.Open();
if(cmd.ExecuteNonQuery()>0)
{
}
else
{
Page.RegisterStartupScript("msg","<script>alert('修改失败!');</script>");
}
cmd.Connection.Close();
this.DataGridCustomer.EditItemIndex =-1;
this.BindGrid();
}
private void DataGridCustomer_EditCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGridCustomer.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
private void DataGridCustomer_SelectedIndexChanged(object sender, System.EventArgs e)
{
string id = this.DataGridCustomer.SelectedItem.Cells[1].Text;
Response.Redirect("Viewcustomer.aspx?id="+id);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -