datagriddoubledropdownlist.aspx.cs

来自「asp.net专家200问(含源代码解决法案」· CS 代码 · 共 157 行

CS
157
字号
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 CommonFunction
{
	/// <summary>
	/// DataGridDoubleDropDownList 的摘要说明。
	/// </summary>
	public class DataGridDoubleDropDownList : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid dgDoubleDrp;
		public string orderID = "";
		protected string productID = "";
		SqlConnection conn = new SqlConnection();
		SqlCommand cmd = new SqlCommand();
		private void Page_Load(object sender, System.EventArgs e)
		{
			//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
			conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
			//页面初试化时进行数据绑定
			if(!IsPostBack)
				DataGridDataBind();
			
		}
		
		//进行数据绑定
		private void DataGridDataBind()
		{
			
			//创建数据适配器对象
			SqlDataAdapter da = new SqlDataAdapter("select top 10 Orders.OrderID,ProductID from Orders,[Order Details] where Orders.OrderID = [Order Details].OrderID order by Orders.OrderID",conn);
			//创建DataSet对象
			DataSet ds = new DataSet();
			try
			{
				//填充数据集
				da.Fill(ds,"testTable");
				//进行数据绑定
				dgDoubleDrp.DataSource = ds.Tables["testTable"];
				dgDoubleDrp.DataBind();
			}
			catch(SqlException error)
			{
				//输出异常信息
				Response.Write(error.ToString());
			}		
		}
		public SqlDataReader BindTheOrderID()
		{
			//定义命令对象
			cmd =new SqlCommand("select OrderID from Orders order by OrderID", conn);
			//打开数据库联结
			conn.Open();
			//返回DataReader对象
			return cmd.ExecuteReader(CommandBehavior.CloseConnection);			
		}
		public SqlDataReader BindTheProductID(string OrderID)
		{
			string strSql = "";

			if(OrderID != "" && OrderID != null)
			{
				strSql = "select ProductID from [Order Details] where OrderID = '"+OrderID+"'";
			}
			else
			{
				strSql = "select distinct ProductID from [Order Details]";	
			}
			//定义命令对象
			cmd =new SqlCommand(strSql, conn);
			//打开数据库联结
			conn.Open();
			//返回DataReader对象
			return cmd.ExecuteReader(CommandBehavior.CloseConnection);			
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgDoubleDrp.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_CancelCommand);
			this.dgDoubleDrp.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgDoubleDrp_EditCommand);
			this.dgDoubleDrp.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgDoubleDrp_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dgDoubleDrp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//取得当前行的订单号和商品号
			orderID = ((Label) e.Item.FindControl("lblOrderID")).Text.Trim();
			productID = ((Label) e.Item.FindControl("lblproductID")).Text.Trim();
			//设置DataGrid控件的当前编辑项的索引
			dgDoubleDrp.EditItemIndex = e.Item.ItemIndex;
			//重新进行数据绑定
			DataGridDataBind();
		}

		public void DropDownListChange(object sender, System.EventArgs e)
		{
			//建立drpOrder下拉列表控件的引用对象
			DropDownList OrderDrp = (DropDownList) sender;
			//取得选择的订单号
			orderID = OrderDrp.SelectedItem.Text;
			//重新进行数据绑定
			DataGridDataBind();
		}

		private void dgDoubleDrp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			//取消编辑
			dgDoubleDrp.EditItemIndex = -1;
			DataGridDataBind();
		}

		private void dgDoubleDrp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			//判断是否是编辑项
			if (e.Item.ItemType == ListItemType.EditItem) 
			{
				//找到编辑项中的drpOrder下拉列表控件
				DropDownList drpOrderID = (DropDownList) e.Item.FindControl("drpOrder");
				//设置在上个操作中drpOrder下拉列表中选中的项
				drpOrderID.SelectedIndex = drpOrderID.Items.IndexOf(drpOrderID.Items.FindByText(orderID));

				//找到编辑项中的drpProduct下拉列表控件
				DropDownList drpProductID = (DropDownList) e.Item.FindControl("drpProduct");
				//设置在上个操作中drpProduct下拉列表中选中的项
				drpProductID.SelectedIndex = drpProductID.Items.IndexOf(drpProductID.Items.FindByText(productID));
			}
		}

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?