📄 wf_total_menu.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;
namespace WebApplication1 //HN_TOTAl_MEMU // HNtest1_main1
{
/// <summary>
/// WFapp 的摘要说明。
/// </summary>
public class WF_TOTAL_MEMU : System.Web.UI.Page
{
protected System.Data.DataView dataView1;
protected System.Data.Odbc.OdbcConnection odbcConnection1;
protected System.Data.Odbc.OdbcCommand odbcSelectCommand1;
internal System.Data.Odbc.OdbcCommand odbcInsertCommand1;
protected System.Data.Odbc.OdbcCommand odbcUpdateCommand1;
protected System.Data.Odbc.OdbcCommand odbcDeleteCommand1;
protected System.Data.Odbc.OdbcDataAdapter odbcDataAdapter1;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.DataSet dataSet1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Data.Odbc.OdbcCommand odbcCommand1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.RangeValidator valPrice;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button7;
// protected System.Data.DataSet dataSet1;
private string selectstr;
private string Insertstr;
private string strUser ;
private string strUserName;
private string strDept;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.Label lblMeage;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private string strDeptName;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
strUser = (string)Session["strUser"];
strUserName = (string)Session["strUserName"];
strDept = (string)Session["strDept"];
strDeptName = (string)Session["strDeptName"];
if (!IsPostBack)
{
if (!Global.CheckUserValidation((string)Session["strRole"], Global.MENU_TOTALMENU))
{
Response.Redirect("err_session.htm", true);
}
DataGrid1.CurrentPageIndex=0;
DataGrid1.PageSize = Convert.ToInt32 (TextBox1.Text );
select();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataView1 = new System.Data.DataView();
this.odbcConnection1 = new System.Data.Odbc.OdbcConnection();
this.odbcSelectCommand1 = new System.Data.Odbc.OdbcCommand();
this.odbcInsertCommand1 = new System.Data.Odbc.OdbcCommand();
this.odbcUpdateCommand1 = new System.Data.Odbc.OdbcCommand();
this.odbcDeleteCommand1 = new System.Data.Odbc.OdbcCommand();
this.odbcDataAdapter1 = new System.Data.Odbc.OdbcDataAdapter();
this.dataSet1 = new System.Data.DataSet();
this.odbcCommand1 = new System.Data.Odbc.OdbcCommand();
((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged_1);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Button5.Click += new System.EventHandler(this.Button5_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Button7.Click += new System.EventHandler(this.Button7_Click);
//
// odbcDataAdapter1
//
this.odbcDataAdapter1.DeleteCommand = this.odbcDeleteCommand1;
this.odbcDataAdapter1.InsertCommand = this.odbcInsertCommand1;
this.odbcDataAdapter1.SelectCommand = this.odbcSelectCommand1;
this.odbcDataAdapter1.UpdateCommand = this.odbcUpdateCommand1;
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
}
#endregion
private void connectionDB()
{
Global.OpenConnection(ref odbcConnection1);
}
private void closeDB()
{
odbcConnection1.Close();
}
private void select()
{
//connectionDB();
DataGrid1.CurrentPageIndex=0;
DataGrid1.PageSize = Convert.ToInt32 (TextBox1.Text );
selectdata();
PageIndexNbr();
}
private void selectdata()
{
connectionDB();
// selectstr="select food_id as '菜式编号',Style as '类型',name as '菜名',Price as '价格',Description as '描述',Image as '电子图片',Effect as '是否有效' from T_total_menu order by food_id desc";
selectstr="select food_id ,Style ,name ,Price ,Description ,Image ,Effect from T_total_menu order by food_id desc";
odbcDataAdapter1.SelectCommand.CommandText = selectstr;
odbcDataAdapter1.SelectCommand.Connection = odbcConnection1;
odbcDataAdapter1.Fill ( dataSet1 , "T_total_menu" ) ;
DataView dataView1 = dataSet1.Tables [ "T_total_menu" ].DefaultView ;
DataGrid1.DataSource = dataView1 ;
DataGrid1.DataBind() ;
//odbcConnection1.Close();
closeDB();
DataGrid1.HeaderStyle.Height=30;
}
private void PageIndexNbr()
{
Label1.Text = "第 " + (DataGrid1.CurrentPageIndex + 1).ToString() + " 页/共 " + DataGrid1.PageCount.ToString() + " 页";
}
private void DataGrid1_PageIndexChanged_1(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
selectdata();
PageIndexNbr();
}
private void Button5_Click(object sender, System.EventArgs e)
{
lblMeage.Text = "";
if ((!valPrice.IsValid)||(TextBox3.Text==""))
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:价格格式错误!必须为整数!"")</script>");
return;
}
string str1= DropDownList1.SelectedItem.Value;
str1="'"+str1+"','"+TextBox2.Text+"',"+TextBox3.Text+",'"+TextBox4.Text+"','"+TextBox5.Text+"',"+DropDownList2.SelectedItem.Value;
Insertstr="INSERT INTO T_total_menu(`Style` ,`name` ,`Price`,`Description`,`Image` ,`Effect`) VALUES ( "+str1+")";
//TextBox6.Text=Insertstr;
insertdata();
DataGrid1.SelectedIndex = -1;
//Button1_Click(sender, e);
Global.WriteLog(ref odbcConnection1, strUser,Global.MENU_TOTALMENU,Global.ACTION_ADD,Insertstr);
select();
}
private void insertdata()
{
int i;
connectionDB();
odbcInsertCommand1.CommandText = Insertstr;
odbcInsertCommand1.Connection = odbcConnection1;
i = odbcInsertCommand1.ExecuteNonQuery ();
if (i != 1)
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:增加数据错误!"")</script>");
// lblMeage.Text = "发生错误。增加数据错误!";
}
closeDB();
}
private void Button7_Click(object sender, System.EventArgs e)
{
select();
DropDownList1.SelectedIndex=0;
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
TextBox5.Text="";
DropDownList2.SelectedIndex=1;
lblMeage.Text = "";
Insertstr="";
}
//Modify
private void Button2_Click(object sender, System.EventArgs e)
{
string str1= DropDownList1.SelectedItem.Value;
string strUpdate;
int iUpdateRowCount;
lblMeage.Text = "";
if ((!valPrice.IsValid)||(TextBox3.Text==""))
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:价格格式错误!必须为整数!"")</script>");
return;
}
strUpdate = "UPDATE T_total_menu set " +
"style = '" + DropDownList1.SelectedItem.Value + "', " +
"name = '" + TextBox2.Text + "', " +
"price = " + TextBox3.Text + ", " +
"Description = '" + TextBox4.Text + "'," +
"Image = '" + TextBox5.Text + "'," +
"Effect = '" + DropDownList2.SelectedItem.Value + "'" +
"where food_id = ";
if (DataGrid1.SelectedIndex < 0) //unselected
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:未选择记录!请先选择记录,以对其进行修改!"")</script>");
// lblMeage.Text = "未选择一条记录!请先选择记录,以对其进行修改。";
return;
}
else
{
strUpdate += DataGrid1.SelectedItem .Cells [1].Text;
}
connectionDB();
odbcCommand1.CommandText = strUpdate;
odbcCommand1.Connection = odbcConnection1;
iUpdateRowCount = odbcCommand1.ExecuteNonQuery ();
if (iUpdateRowCount != 1)
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:未能更改数据!"")</script>");
return;
}
else
{
DataGrid1.SelectedIndex = -1;
Global.WriteLog(ref odbcConnection1, strUser,Global.MENU_TOTALMENU,Global.ACTION_UPDATE,strUpdate);
select();
}
closeDB();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList1.SelectedValue = ((DropDownList)DataGrid1.SelectedItem.Cells[2].Controls[1]).SelectedValue ; //style
TextBox2.Text = DataGrid1.SelectedItem .Cells [3].Text ; //name
TextBox3.Text = DataGrid1.SelectedItem .Cells [4].Text ; //price
TextBox4.Text = DataGrid1.SelectedItem .Cells [5].Text ; //desc
TextBox5.Text = DataGrid1.SelectedItem .Cells [6].Text ; //img
// DropDownList1.SelectedValue = DataGrid1.SelectedItem .Cells [2].Text; //style
// DropDownList2.SelectedValue = DataGrid1.SelectedItem .Cells [7].Text; //effect
DropDownList2.SelectedValue =((DropDownList) DataGrid1.SelectedItem.Cells[7].Controls[1]).SelectedValue; //effect
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
string strUpdate;
int iUpdateRowCount;
lblMeage.Text = "";
if (DataGrid1.SelectedIndex < 0) //unselected
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:未选择一条记录!请先选择记录,以对其进行删除!"")</script>");
// lblMeage.Text = "未选择一条记录!请先选择记录,以对其进行修改。";
return;
}
strUpdate = "DELETE FROM T_total_menu where food_id = ";
strUpdate += DataGrid1.SelectedItem.Cells[1].Text;
//lblMeage.Text = strUpdate;
connectionDB();
odbcCommand1.CommandText = strUpdate;
odbcCommand1.Connection = odbcConnection1;
iUpdateRowCount = odbcCommand1.ExecuteNonQuery ();
closeDB();
if (iUpdateRowCount != 1)
{
Page.RegisterStartupScript("alert", @"<script language=""javascript"">window.alert(""错误:未能删除数据!"")</script>");
return;
}
else
{
DataGrid1.SelectedIndex = -1;
Global.WriteLog(ref odbcConnection1, strUser,Global.MENU_TOTALMENU,Global.ACTION_UPDATE,strUpdate);
select();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -