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

📄 defaultcs.aspx.cs

📁 Telerik是很大的第三方软件制造商
💻 CS
字号:
using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.QuickStart;
using Telerik.WebControls;

namespace Telerik.GridExamplesCSharp.Programming.CustomizingPager
{
	public class DefaultCS : XhtmlPage
	{
		protected System.Web.UI.WebControls.Button Button1;
		protected Telerik.WebControls.RadGrid RadGrid1;

		private DataTable GetDataSource()
		{
			OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"));
			OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
			MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, PostalCode FROM Customers", MyOleDbConnection);

			DataTable myDataTable = new DataTable();

			MyOleDbConnection.Open();
			try
			{
				MyOleDbDataAdapter.Fill(myDataTable);
			}
			finally
			{
				MyOleDbConnection.Close();
			}

			return myDataTable;
		}

		#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.RadGrid1.NeedDataSource += new Telerik.WebControls.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
			this.RadGrid1.ItemEvent += new Telerik.WebControls.GridItemEventHandler(this.RadGrid1_ItemEvent);
		}
		#endregion

		private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
		{
			this.RadGrid1.DataSource = this.GetDataSource();
		}

		private void RadGrid1_ItemEvent(object sender, Telerik.WebControls.GridItemEventArgs e)
		{
			if ( e.EventInfo is GridInitializePagerItem )
			{
				GridInitializePagerItem info = e.EventInfo as GridInitializePagerItem;
				e.Canceled = true;

				(e.Item as GridPagerItem).PagerContentCell.Controls.Add( 
					new MyPager(info.PagingManager, e.Item.OwnerTableView) 
					);
			}
		}

		private class MyPager: Control
		{
			private GridPagingManager paging;
			private GridTableView tableView;

			TextBox textBox;
			RangeValidator rangeValidator;

			public MyPager( GridPagingManager paging, GridTableView tableView )
			{
				this.paging = paging;
				this.tableView = tableView;

				this.EnsureChildControls();
			}

			protected override void CreateChildControls()
			{
				this.Controls.Add( new LiteralControl("Page: ") );

				textBox = new TextBox();
				textBox.ID = "textbox";
				textBox.Columns = 3;
				
				this.Controls.Add( textBox );

				this.Controls.Add( new LiteralControl(" (of " + this.paging.PageCount + ") ") );

				LinkButton btn = new LinkButton();
				btn.ID = "btn1";
				btn.Text = "Change page";
				btn.Click += new EventHandler( this.BtnClick );

				this.Controls.Add( btn );

				rangeValidator = new RangeValidator();
				rangeValidator.ErrorMessage = " Page index should be in the range of 1 to " + this.paging.PageCount;
				
				this.Controls.Add(rangeValidator);
			}

			protected override void OnPreRender(EventArgs e)
			{
				textBox.Text = (paging.CurrentPageIndex + 1).ToString();

				rangeValidator.Type = ValidationDataType.Integer;
				rangeValidator.MinimumValue = 1.ToString();
				rangeValidator.MaximumValue = this.paging.PageCount.ToString();
				
				rangeValidator.ControlToValidate = textBox.ID;

				base.OnPreRender (e);
			}

			private void BtnClick(object sender, EventArgs e)
			{
				if ( this.textBox.Text.Trim() != "" )
				{
					this.tableView.CurrentPageIndex = int.Parse( this.textBox.Text ) - 1;
					this.tableView.Rebind();
				}
			}
		}
	}
}

⌨️ 快捷键说明

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