📄 editsale.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace SellManageJane
{
/// <summary>
/// Editsale 的摘要说明。
/// </summary>
public class Editsale : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.TextBox Txtkey;
protected System.Web.UI.WebControls.Button BtnFound;
protected System.Web.UI.WebControls.DataGrid DataGridsale;
SqlConnection conn;
SqlDataAdapter ada;
SqlCommand cmd;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
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();
this.DropDownList1.Items.Insert(0,new ListItem("任意条件","*"));
}
}
private void BindGrid()
{
ada = new SqlDataAdapter("Select * from saleView",conn);
ada.Fill(ds,"Sale");
this.DataGridsale.DataSource=ds.Tables["Sale"].DefaultView;
this.DataGridsale.DataKeyField = "Id";
this.DataBind();
}
private void InitializeComponent()
{
this.BtnFound.Click += new System.EventHandler(this.BtnFound_Click);
this.DataGridsale.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGridsale_PageIndexChanged);
this.DataGridsale.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridsale_CancelCommand);
this.DataGridsale.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridsale_EditCommand);
this.DataGridsale.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridsale_UpdateCommand);
this.DataGridsale.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridsale_DeleteCommand);
this.DataGridsale.SelectedIndexChanged += new System.EventHandler(this.DataGridsale_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void DataGridsale_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGridsale.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void DataGridsale_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGridsale.EditItemIndex = -1;
BindGrid();
}
private void DataGridsale_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGridsale.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
private void DataGridsale_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
cmd = new SqlCommand("delete from saleView where id=@id",conn);
cmd.Parameters.Add("@id",SqlDbType.Int);
cmd.Parameters["@id"].Value = int.Parse(this.DataGridsale.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)
{
string msg = ex.Message.ToString();
RegisterStartupScript("errmsg","<script>alert('删除失败,DELETE 语句与 COLUMN REFERENCE 约束 FK__Sale__customerId__023D5A04 冲突。该冲突发生于数据库 SellManage,表 Sale, column customerId。语句已终止。');</script>");
}
}
private void DataGridsale_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
cmd = new SqlCommand("updateSaleInfo",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID",SqlDbType.Int);
cmd.Parameters.Add("@sale_goodsId",SqlDbType.VarChar,20);
cmd.Parameters.Add("@customerId",SqlDbType.VarChar,30);
cmd.Parameters.Add("@title",SqlDbType.VarChar,255);
cmd.Parameters.Add("@price",SqlDbType.Float,8);
cmd.Parameters.Add("@costPrice",SqlDbType.Float,8);
cmd.Parameters.Add("@amount",SqlDbType.Int);
cmd.Parameters.Add("@salesDate",SqlDbType.DateTime);
cmd.Parameters.Add("@remark",SqlDbType.VarChar,250);
cmd.Parameters["@ID"].Value = int.Parse(this.DataGridsale.DataKeys[e.Item.ItemIndex].ToString());
cmd.Parameters["@sale_goodsId"].Value =((TextBox)e.Item.Cells[2].Controls[0]).Text ;
cmd.Parameters["@customerId"].Value =((TextBox)e.Item.Cells[3].Controls[0]).Text ;
cmd.Parameters["@title"].Value =((TextBox)e.Item.Cells[4].Controls[0]).Text;
cmd.Parameters["@price"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
cmd.Parameters["@costPrice"].Value = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
cmd.Parameters["@amount"].Value = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
cmd.Parameters["@salesDate"].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)
{
//Page.RegisterStartupScript("msg","<script>alert('修改成功!');</script>");
}
else
{
Page.RegisterStartupScript("msg","<script>alert('修改失败!');</script>");
}
cmd.Connection.Close();
this.DataGridsale.EditItemIndex =-1;
this.BindGrid();
}
catch(Exception ex)
{
string err = ex.Message;
Page.RegisterStartupScript("msg","<script>alert('修改失败!请重试!');</script>");
}
}
private void DataGridsale_SelectedIndexChanged(object sender, System.EventArgs e)
{
string id = this.DataGridsale.SelectedItem.Cells[1].Text;
if(id.ToString()!=null)
{
Response.Redirect("Viewsale.aspx?id="+id);
}
else
{
RegisterStartupScript("msg","<script>alert('传入参数不能为空!!!');</script>");
}
}
private void BtnFound_Click(object sender, System.EventArgs e)
{
try
{
if(DropDownList1.SelectedValue=="产品编号")
{
ada = new SqlDataAdapter("select * from saleView where sale_goodsId like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"sale_goodsId");
if(ds.Tables["sale_goodsId"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["sale_goodsId"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="客户编号")
{
ada = new SqlDataAdapter("select * from saleView where customerId like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"customerId");
if(ds.Tables["customerId"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["customerId"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="销售数量")
{
ada = new SqlDataAdapter("select * from saleView where amount like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"amount");
if(ds.Tables["amount"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["amount"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="录入时间")
{
ada = new SqlDataAdapter("select * from saleView where salesDate like '%"+ DatePicker1.Value +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"salesDate");
if(ds.Tables["salesDate"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["salesDate"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="*")
{
ada = new SqlDataAdapter("select * from saleView",conn);
ds = new DataSet();
ada.Fill(ds,"Sale1");
if(ds.Tables["Sale1"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["Sale1"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else
{
ada = new SqlDataAdapter("select * from saleView",conn);
ds = new DataSet();
ada.Fill(ds,"Sale");
if(ds.Tables["Sale"].Rows.Count!=0)
{
this.DataGridsale.DataSource = ds.Tables["Sale"].DefaultView;
this.DataGridsale.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -