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

📄 mysingledatabind.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.Configuration;
using System.Data.SqlClient;

namespace Example_9_1
{
	/// <summary>
	/// Summary description for MySingelDataBind.
	/// </summary>
	public class MySingelDataBind : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label MyLabelString;
		protected System.Web.UI.WebControls.Label MyLabelExpr;
		protected System.Web.UI.WebControls.TextBox MyTextBoxString;
		protected System.Web.UI.WebControls.TextBox MyTextboxExpr;
		protected System.Web.UI.WebControls.Button ChangeBtn;
		protected System.Web.UI.WebControls.Button MyButtonString;
		protected System.Web.UI.WebControls.DataList TabList;
		public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();

		public String LabelValueString = "我的Label控件!";
		public string TextBoxValueString = "我的TextBox控件!";
		public bool TextBoxVisibleString = true;
		protected System.Web.UI.WebControls.Button SetControlValue;
		public string ButtonValueString  = "我的Button控件!";


		private void Page_Load(object sender, System.EventArgs e)
		{
			BindControlData();
			BindTabData();
		}

		private void BindControlData()
		{
			///从数据库中获取表记录
			String cmdText = "SELECT TOP 1 * From Staff WHERE Staff_ID = '8'";
			SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
			SqlCommand myCommand = new SqlCommand(cmdText,myConnection);

			///执行数据库查询
			myConnection.Open();
			SqlDataReader dr = myCommand.ExecuteReader();
			if(dr.Read())
			{
				LabelValueString = "我的名称:" + dr["Staff_Name"].ToString();
				TextBoxValueString = "我的Email:" + dr["Email"].ToString();
				ButtonValueString = "我的真实姓名:" + dr["RealName"].ToString();
			}
			dr.Close();
			myConnection.Close();	

			MyLabelString.Text = MyLabelExpr.Text = LabelValueString;
			MyTextboxExpr.Text = MyTextboxExpr.Text = TextBoxValueString;
			MyTextboxExpr.Visible = SetTextBoxVisible();

			MyButtonString.Text = ButtonValueString;
		}

		public bool SetTextBoxVisible()
		{
			return(TextBoxVisibleString);
		}

		private void BindTabData()
		{
			///获取Tabs表中的数据
			TabDB tab = new TabDB();
			SqlDataReader rect = tab.GetTabs();

			///创建TabList控件的数据源
			ArrayList tabData = new ArrayList();
			while(rect.Read())
			{
				///添加一个新的数据项
				TabDetails newdata = new TabDetails();
				newdata.TabId = Int32.Parse(rect["TabID"].ToString());
				newdata.TabName = rect["TabName"].ToString();
				newdata.TabOrder = Int32.Parse(rect["TabOrder"].ToString());

				tabData.Add(newdata);
			}
			rect.Close();

			///绑定控件的数据
			TabList.DataSource = tabData;
			TabList.DataBind();
		}

		#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.ChangeBtn.Click += new System.EventHandler(this.ChangeBtn_Click);
			this.SetControlValue.Click += new System.EventHandler(this.SetControlValue_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ChangeBtn_Click(object sender, System.EventArgs e)
		{
			if(TextBoxVisibleString == true)
			{
				TextBoxVisibleString = false;
			}
			else
			{
				TextBoxVisibleString = true;
			}

			MyTextboxExpr.Visible = SetTextBoxVisible();
		}

		private void SetControlValue_Click(object sender, System.EventArgs e)
		{
			BindControlData();
			BindTabData();		
		}
	}
}

⌨️ 快捷键说明

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