📄 webform1.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>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.DropDownList DropDownList4;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Message;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label Label_error;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//设置数据库连接
if(!IsPostBack)
{
//将4个dropdownlist控件进行数据绑定
//对四个下拉框控件进行初始化
//1
bind_DrpDownList bd1=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=0",DropDownList1);
//2
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);
//3
if(DropDownList2.Items.Count!=0)
{
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);
}
else
{
DropDownList3.Items.Clear();
}
//4
if(DropDownList3.Items.Count!=0)
{
bind_DrpDownList bd4=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')",DropDownList4);
}
else
{
DropDownList4.Items.Clear();
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.DropDownList2.SelectedIndexChanged += new System.EventHandler(this.DropDownList2_SelectedIndexChanged);
this.DropDownList3.SelectedIndexChanged += new System.EventHandler(this.DropDownList3_SelectedIndexChanged);
this.Button1.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)
{
bind_DrpDownList bd=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList1.SelectedItem.Text+"')",DropDownList2);
if(DropDownList2.Items.Count!=0)
{
bind_DrpDownList bd1=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"')",DropDownList3);
if(DropDownList3.Items.Count!=0)
{
bind_DrpDownList bd2=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')",DropDownList4);
}
else
{
DropDownList4.Items.Clear(); //清空第四个下拉列表
}
}
else
{
DropDownList3.Items.Clear(); //清空第三个下拉列表
DropDownList4.Items.Clear(); //清空第四个下拉列表
}
}
//第二个下拉框的选择时间处理
private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
{
bind_DrpDownList bd=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList2.SelectedItem.Text+"')",DropDownList3);
if(DropDownList3.Items.Count!=0)
{
bind_DrpDownList bd1=new bind_DrpDownList("select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')",DropDownList4);
}
else
{
DropDownList4.Items.Clear();
}
}
//第三个下拉框的选择时间处理
private void DropDownList3_SelectedIndexChanged(object sender, System.EventArgs e)
{
String strSQL = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')";
bind_DrpDownList bd=new bind_DrpDownList(strSQL,DropDownList4);
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(DropDownList4.Items.Count!=0)
{
bind_DataGrid bind_dg=new bind_DataGrid(DataGrid1,"select * from sm where TYPE_NAME='"+DropDownList4.SelectedItem.Text+"'");
DataGrid1.Visible=true;
Label_error.Visible=false;
}
else
{
Label_error.Visible=true;
DataGrid1.Visible=false;
}
}
#region DataGrid控件的编辑和删除
//DataGrid编辑和删除函数
public void DataGrid_Edit(Object sender,DataGridCommandEventArgs E)
{
DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
bind_DataGrid bind_dg=new bind_DataGrid(DataGrid1,"select * from sm where TYPE_NAME='"+DropDownList4.SelectedItem.Text+"'");
}
public void DataGrid_Cancel(Object sender,DataGridCommandEventArgs E)
{
DataGrid1.EditItemIndex = -1;
bind_DataGrid bind_dg=new bind_DataGrid(DataGrid1,"select * from sm where TYPE_NAME='"+DropDownList4.SelectedItem.Text+"'");
}
public void DataGrid_Updata(Object sender,DataGridCommandEventArgs E)
{
SqlConnection Conn;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
//定位要编辑的行
String strSQL = "UPDATE sm set PRICE="+((TextBox)E.Item.Cells[4].Controls[0]).Text+" where id='"+DataGrid1.DataKeys[(int)E.Item.ItemIndex]+"'";
SqlCommand cm = new SqlCommand(strSQL,Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.Text="<b>编辑成功</b>";
//String strSQL0 = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')";
//bind_DrpDownList bd=new bind_DrpDownList(strSQL0,DropDownList4);
// 退出编辑状态
DataGrid1.EditItemIndex=-1;
}
catch (SqlException)
{
Message.Text="编辑失败";
Message.Style["color"]="red";
}
// 更新DataGrid
bind_DataGrid bind_dg=new bind_DataGrid(DataGrid1,"select * from sm where TYPE_NAME='"+DropDownList4.SelectedItem.Text+"'");
cm.Connection.Close();
}
// 处理删除事件
public void DataGrid_Delete(Object sender,DataGridCommandEventArgs E)
{
SqlConnection Conn;
Conn=new SqlConnection("server=localhost;database=MyTest;uid=sa;pwd=''");
//定位要删除的行
String strSQL="DELETE FROM sm WHERE id='"+ DataGrid1.DataKeys[(int)E.Item.ItemIndex]+"'";
SqlCommand cm = new SqlCommand(strSQL,Conn);
Conn.Open();
try
{
cm.ExecuteNonQuery();
Message.Text = "<b>删除成功</b>";
String strSQL0 = "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(strSQL0,DropDownList2);
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);
String strSQL2 = "select TYPE_NAME from sm where TYPE_PCODE=(select TYPE_CODE from sm where TYPE_NAME='"+DropDownList3.SelectedItem.Text+"')";
bind_DrpDownList bd2=new bind_DrpDownList(strSQL2,DropDownList4);
DataGrid1.Visible=false;
}
catch (SqlException)
{
Message.Text = "<b>删除失败</b>";
Message.Style["color"]="red";
}
Conn.Close();
}
#endregion
private void LinkButton1_Click(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -