📄 add.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;
namespace Super_Market
{
/// <summary>
/// add 的摘要说明。
/// </summary>
public class add : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Panel Panel21;
protected System.Web.UI.WebControls.Panel Panel22;
protected System.Web.UI.WebControls.Panel Panel23;
protected System.Web.UI.WebControls.ImageButton ImageButton_a3;
protected System.Web.UI.WebControls.TextBox TextBox_name;
protected System.Web.UI.WebControls.TextBox TextBox_price;
protected System.Web.UI.WebControls.ImageButton ImageButton_a2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.ImageButton ImageButton_a1;
protected System.Web.UI.WebControls.Label message1;
protected System.Web.UI.WebControls.ImageButton ImageButton_n3;
protected System.Web.UI.WebControls.ImageButton ImageButton_n4;
protected System.Web.UI.WebControls.Label message2;
protected System.Web.UI.WebControls.Label message3;
protected System.Web.UI.WebControls.Label message4;
protected System.Web.UI.WebControls.Button Button_add;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.ImageButton ImageButton_n2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//绑定第一,二,三个下拉框
if(!IsPostBack)
{
bind_DrpDownList bd1=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=0",DropDownList1);
bind_DrpDownList bd2=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList1.SelectedItem.Text+"')",DropDownList2);
bind_DrpDownList bd3=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"')",DropDownList3);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ImageButton_n2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_n2_Click);
this.ImageButton_n3.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_n3_Click);
this.ImageButton_n4.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_n4_Click);
this.DropDownList2.SelectedIndexChanged += new System.EventHandler(this.DropDownList2_SelectedIndexChanged);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.ImageButton_a2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_a2_Click);
this.ImageButton_a3.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_a3_Click);
this.ImageButton_a1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton_a1_Click);
this.Button_add.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
String strSQL = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList1.SelectedItem.Text+"')";
bind_DrpDownList bd=new bind_DrpDownList(strSQL,DropDownList2);
if(DropDownList2.Items.Count!=0)
{
String strSQL1 = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"')";
bind_DrpDownList bd1=new bind_DrpDownList(strSQL1,DropDownList3);
}
else
{
DropDownList3.Items.Clear();
}
}
private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
{
String strSQL1 = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"')";
bind_DrpDownList bd1=new bind_DrpDownList(strSQL1,DropDownList3);
}
#region 添加按钮的图像切换功能
//添加按钮的图像切换功能
private void ImageButton_a1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Panel21.Visible==false)
{
Panel21.Visible=true;
}
else
{
Panel21.Visible=false;
}
}
private void ImageButton_a2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Panel22.Visible==false)
{
Panel22.Visible=true;
}
else
{
Panel22.Visible=false;
}
}
private void ImageButton_a3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Panel23.Visible==false)
{
Panel23.Visible=true;
}
else
{
Panel23.Visible=false;
}
}
#endregion
private void ImageButton_n2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//添加一个一级类名
if(TextBox1.Text!="")
{
SqlConnection Conn;
int code,xh,pcode=0;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
Conn.Open();
SqlCommand cm1= new SqlCommand("select max(TYPE_CODE) from sm where TYPE_PCODE=0",Conn);
SqlDataReader dr1=cm1.ExecuteReader();
try
{
dr1.Read();
code=(int)dr1[0]+1;
}
catch
{
code=pcode*10+1;
}
dr1.Close();
SqlCommand cm2= new SqlCommand("select max(TYPE_XH) from sm where TYPE_PCODE=0",Conn);
SqlDataReader dr2=cm2.ExecuteReader();
try
{
dr2.Read();
xh=(int)dr2[0]+1;
}
catch
{
xh=1;
}
dr2.Close();
String strSQL =@"insert into sm(TYPE_CODE,TYPE_PCODE,TYPE_NAME,TYPE_XH) values("+code+",0,'"+TextBox1.Text+"',"+xh+")";
SqlCommand cm = new SqlCommand(strSQL,Conn);
try
{
cm.ExecuteNonQuery();
message1.Text="<b>添加成功</b>";
message1.Style["color"]="black";
//绑定第一个下拉框
bind_DrpDownList bd=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=0",DropDownList1);
}
catch (SqlException)
{
message1.Text="添加失败";
message1.Style["color"]="red";
}
}
else
{
message1.Text="类名不能为空!";
message1.Style["color"]="red";
}
}
private void ImageButton_n3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//添加一个二级类名
if(TextBox2.Text!=""&&DropDownList1.Items.Count!=0)
{
SqlConnection Conn;
int code,xh,pcode;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
Conn.Open();
SqlCommand cm1= new SqlCommand("select TYPE_CODE from sm where TYPE_NAME='"+DropDownList1.SelectedItem.Text+"'",Conn);
SqlDataReader dr1=cm1.ExecuteReader();
dr1.Read();
pcode=(int)dr1[0];
dr1.Close();
SqlCommand cm2= new SqlCommand("select max(TYPE_CODE) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr2=cm2.ExecuteReader();
try
{
dr2.Read();
code=(int)dr2[0]+1;
}
catch
{
code=pcode*10+1;
}
dr2.Close();
SqlCommand cm3= new SqlCommand("select max(TYPE_XH) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr3=cm3.ExecuteReader();
try
{
dr3.Read();
xh=(int)dr3[0]+1;
}
catch
{
xh=1;
}
dr3.Close();
String strSQL =@"insert into sm(TYPE_CODE,TYPE_PCODE,TYPE_NAME,TYPE_XH) values("+code+","+pcode+",'"+TextBox2.Text+"',"+xh+")";
SqlCommand cm = new SqlCommand(strSQL,Conn);
try
{
cm.ExecuteNonQuery();
message2.Text="<b>添加成功</b>";
message2.Style["color"]="black";
//绑定第二个下拉框
bind_DrpDownList bd=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE="+pcode,DropDownList2);
}
catch (SqlException)
{
message2.Text="添加失败";
message2.Style["color"]="red";
}
}
else if(DropDownList1.Items.Count==0)
{
message2.Text="上级类名呢?";
message2.Style["color"]="red";
}
else
{
message2.Text="类名不能为空!";
message2.Style["color"]="red";
}
}
private void ImageButton_n4_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//添加一个三级类名
if(TextBox3.Text!=""&&DropDownList2.Items.Count!=0)
{
SqlConnection Conn;
int code,xh,pcode;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
Conn.Open();
SqlCommand cm1= new SqlCommand("select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"'",Conn);
SqlDataReader dr1=cm1.ExecuteReader();
dr1.Read();
pcode=(int)dr1[0];
dr1.Close();
SqlCommand cm2= new SqlCommand("select max(TYPE_CODE) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr2=cm2.ExecuteReader();
try
{
dr2.Read();
code=(int)dr2[0]+1;
}
catch
{
code=pcode*10+1;
}
dr2.Close();
SqlCommand cm3= new SqlCommand("select max(TYPE_XH) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr3=cm3.ExecuteReader();
try
{
dr3.Read();
xh=(int)dr3[0]+1;
}
catch
{
xh=1;
}
dr3.Close();
String strSQL =@"insert into sm(TYPE_CODE,TYPE_PCODE,TYPE_NAME,TYPE_XH) values("+code+","+pcode+",'"+TextBox3.Text+"',"+xh+")";
SqlCommand cm = new SqlCommand(strSQL,Conn);
try
{
cm.ExecuteNonQuery();
message3.Text="<b>添加成功</b>";
message3.Style["color"]="black";
//绑定第三个下拉框
bind_DrpDownList bd=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE="+pcode,DropDownList3);
}
catch (SqlException)
{
message3.Text="添加失败";
message3.Style["color"]="red";
}
}
else if(DropDownList2.Items.Count==0)
{
message3.Text="上级类名呢?";
message3.Style["color"]="red";
}
else
{
message3.Text="类名不能为空!";
message3.Style["color"]="red";
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
//添加商品名称和商品价格
if(TextBox_name.Text!=""&&TextBox_price.Text!=""&&DropDownList3.Items.Count!=0)
{
SqlConnection Conn;
int code,xh,pcode;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
Conn.Open();
SqlCommand cm1= new SqlCommand("select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"'",Conn);
SqlDataReader dr1=cm1.ExecuteReader();
dr1.Read();
pcode=(int)dr1[0];
dr1.Close();
SqlCommand cm2= new SqlCommand("select max(TYPE_CODE) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr2=cm2.ExecuteReader();
try
{
dr2.Read();
code=(int)dr2[0]+1;
}
catch
{
code=pcode*10+1;
}
dr2.Close();
SqlCommand cm3= new SqlCommand("select max(TYPE_XH) from sm where TYPE_PCODE="+pcode,Conn);
SqlDataReader dr3=cm3.ExecuteReader();
try
{
dr3.Read();
xh=(int)dr3[0]+1;
}
catch
{
xh=1;
}
dr3.Close();
String strSQL =@"insert into sm(TYPE_CODE,TYPE_PCODE,TYPE_NAME,TYPE_XH,PRICE) values("+code+","+pcode+",'"+TextBox_name.Text+"',"+xh+",'"+TextBox_price.Text+"')";
SqlCommand cm = new SqlCommand(strSQL,Conn);
try
{
cm.ExecuteNonQuery();
message4.Text="<b>添加成功</b>";
message4.Style["color"]="black";
}
catch (SqlException)
{
message4.Text="添加失败";
message4.Style["color"]="red";
}
}
else if(DropDownList3.Items.Count==0)
{
message4.Text="上级类名呢?";
message4.Style["color"]="red";
}
else
{
message4.Text="商品名称或商品价格不能为空!";
message4.Style["color"]="red";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -