webform1.aspx.cs

来自「控件datagridSource」· CS 代码 · 共 74 行

CS
74
字号
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DataGridTemplates2
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid myDataGrid;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if( !Page.IsPostBack )
				BindData();
		}

		private void BindData()
		{
			SqlConnection con = new SqlConnection( "server=localhost;integrated security=true;database=Northwind" );
			SqlCommand cmd = new SqlCommand( "SELECT *, 'http://www.' + CustomerID + '.com' As Url FROM Customers", con );
			try
			{
				con.Open();
				myDataGrid.DataSource = cmd.ExecuteReader();
				myDataGrid.DataBind();
				con.Close();
			}
			catch( Exception ) {}
			if( con != null && con.State == ConnectionState.Open )
				con.Close();
		}

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

		}
		#endregion

		private void myDataGrid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if( e.CommandName == "GetDetails" )
				Response.Redirect( "WebForm2.aspx?id=" + e.Item.Cells[0].Text );
		}
	}
}

⌨️ 快捷键说明

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