⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytransaction.aspx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 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;
using System.Configuration;

namespace Example_7_9
{
	/// <summary>
	/// Summary description for MyTransaction.
	/// </summary>
	public class MyTransaction : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ListBox TabList;
		protected System.Web.UI.WebControls.Label SucessMessage;
		protected System.Web.UI.WebControls.ImageButton upBtn;
		protected System.Web.UI.WebControls.ImageButton downBtn;
		private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				///绑定TabList控件的数据
				GetTabs();
			}
		}

		private void GetTabs()
		{
			///从数据库中Tabs表获取所有记录
			String cmdText = "SELECT * FROM Tabs ORDER BY TabOrder";
			SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
			SqlCommand myCommand = new SqlCommand(cmdText,myConnection);

			///执行数据库查询
			myConnection.Open();
			SqlDataReader dr = myCommand.ExecuteReader();
			
			///绑定TabList控件的数据
			TabList.DataSource = dr;
			TabList.DataTextField = "TabName";
			TabList.DataValueField = "TabID";
			TabList.DataBind();

			///关闭读取器和数据库的链接
			dr.Close();
			myConnection.Close();
		}

		private void MoveUpDown_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			String commandName = ((ImageButton)sender).CommandName;

			if(TabList.SelectedIndex > -1)
			{
				switch(commandName)
				{
					case "up":   ///上移
					{
						if(TabList.SelectedIndex > 0)
						{
							UpdateTabOrder(Int32.Parse(TabList.SelectedValue),commandName);
						}
						break;
					}
					case "down":  ///下移
					{
						if(TabList.SelectedIndex < TabList.Items.Count -1)
						{
							UpdateTabOrder(Int32.Parse(TabList.SelectedValue),commandName);
						}
						break;
					}
					default:
					{
						break;
					}
				}
				///重新绑定TabList控件的数据
				GetTabs();
			}
			else
			{
				///显示添加操作的结果信息
				SucessMessage.Text = "请选择列表中的数据项!";
				SucessMessage.Visible = true;
			}		
		}

		private void UpdateTabOrder(int nTabID,String sMoveFlag)
		{
			//定义数据库的Connection and Command 
			SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
			SqlCommand myCommand = new SqlCommand("Pr_UpdateTabOrder",myConnection);

			//定义访问数据库的方式为存储过程
			myCommand.CommandType = CommandType.StoredProcedure;

			//创建访问数据库的参数
			SqlParameter parameterTabID = new SqlParameter("@TabID",SqlDbType.Int,4);
			parameterTabID.Value = nTabID;
			myCommand.Parameters.Add(parameterTabID);

			SqlParameter parameterMoveFlag = new SqlParameter("@MoveFlag",SqlDbType.VarChar,20);
			parameterMoveFlag.Value = sMoveFlag;
			myCommand.Parameters.Add(parameterMoveFlag);

			try
			{
				//打开数据库的连接
				myConnection.Open();
			}
			catch(Exception ex)
			{
				throw new Exception("数据库连接失败!",ex);
			}

			try 
			{
				//执行数据库的存储过程(访问数据库)
				myCommand.ExecuteNonQuery();
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message,ex);
			}
			finally 
			{
				if (myConnection.State == ConnectionState.Open)
				{
					//关闭数据库的连接
					myConnection.Close();
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.upBtn.Click += new System.Web.UI.ImageClickEventHandler(this.MoveUpDown_Click);
			this.downBtn.Click += new System.Web.UI.ImageClickEventHandler(this.MoveUpDown_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion		
	}
}

⌨️ 快捷键说明

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