📄 editgoods.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>
/// Editgoods 的摘要说明。
/// </summary>
public class Editgoods : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.TextBox Txtkey;
protected System.Web.UI.WebControls.Button BtnFound;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
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();
this.DropDownList1.Items.Insert(0,new ListItem("任意条件","*"));
}
}
private void BindGrid()
{
ada = new SqlDataAdapter("select * from Goods",conn);
ada.Fill(ds,"Goods");
this.DataGrid1.DataSource=ds.Tables["Goods"].DefaultView;
this.DataGrid1.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.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.BtnFound.Click += new System.EventHandler(this.BtnFound_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.BindGrid();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.BindGrid();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
this.BindGrid();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
cmd = new SqlCommand("delete from Goods where id=@id",conn);
cmd.Parameters.Add("@id",SqlDbType.Int);
cmd.Parameters["@id"].Value = int.Parse(this.DataGrid1.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 DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
cmd = new SqlCommand("updateGoodsInfo",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@goodsId",SqlDbType.VarChar,20);
cmd.Parameters.Add("@goodsName",SqlDbType.VarChar,50);
cmd.Parameters.Add("@goodsUnit",SqlDbType.VarChar,25);
cmd.Parameters.Add("@price",SqlDbType.Float,8);
cmd.Parameters.Add("@remain",SqlDbType.Float,8);
cmd.Parameters.Add("@images",SqlDbType.VarChar,255);
cmd.Parameters.Add("@time",SqlDbType.DateTime);
cmd.Parameters.Add("@remark",SqlDbType.VarChar,250);
cmd.Parameters["@goodsId"].Value =((TextBox)e.Item.Cells[2].Controls[0]).Text ;
cmd.Parameters["@goodsName"].Value =((TextBox)e.Item.Cells[3].Controls[0]).Text ;
cmd.Parameters["@goodsUnit"].Value =((TextBox)e.Item.Cells[4].Controls[0]).Text;
cmd.Parameters["@price"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
cmd.Parameters["@remain"].Value = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
cmd.Parameters["@images"].Value = ((TextBox)e.Item.Cells[7].Controls[0]).Text;
cmd.Parameters["@time"].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.DataGrid1.EditItemIndex =-1;
this.BindGrid();
}
catch(Exception ex)
{
Page.RegisterStartupScript("msg","<script>alert('修改失败!请重试!');</script>");
}
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string id = this.DataGrid1.SelectedItem.Cells[1].Text;
if(id.ToString()!=null)
{
Response.Redirect("Viewgoods.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 Goods where goodsName like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"goodsName");
if(ds.Tables["goodsName"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["goodsName"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="产品编号")
{
ada = new SqlDataAdapter("select * from Goods where goodsId like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"goodsId");
if(ds.Tables["goodsId"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["goodsId"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="产品单位")
{
ada = new SqlDataAdapter("select * from Goods where goodsUnit like '%"+ Txtkey.Text +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"goodsUnit");
if(ds.Tables["goodsUnit"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["goodsUnit"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="录入时间")
{
ada = new SqlDataAdapter("select * from Goods where Time like '%"+ DatePicker1.Value +"%'",conn);
ds = new DataSet();
ada.Fill(ds,"Time");
if(ds.Tables["Time"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["Time"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else if(DropDownList1.SelectedValue=="*")
{
ada = new SqlDataAdapter("select * from Goods",conn);
ds = new DataSet();
ada.Fill(ds,"Goods1");
if(ds.Tables["Goods1"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["Goods1"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
else
{
ada = new SqlDataAdapter("select * from Goods",conn);
ds = new DataSet();
ada.Fill(ds,"Goods");
if(ds.Tables["Goods"].Rows.Count!=0)
{
this.DataGrid1.DataSource = ds.Tables["Goods"].DefaultView;
this.DataGrid1.DataBind();
}
else
{
RegisterStartupScript("msg","<script>alert('没有您要查找的信息!');</script>");
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -