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

📄 sort-find-datagrid.aspx

📁 Professional ASP.NET source code
💻 ASPX
字号:
<%@Page Language="C#"%>

<%@Import Namespace="System.Data" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Sorting Rows and Finding Data in a DataGrid Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}

.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
        font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
        font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}

</style></head>
<body bgcolor="#ffffff">
<span class="heading">Sorting Rows and Finding Data in a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<%-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server" />

<form runat="server">

  Select only Titles containing the text:
  <ASP:TextBox id="txtFindText" runat="server" />
  <ASP:Button id="cmdFind" Text="Find" runat="server" /><p />

  <ASP:DataGrid id="MyDataGrid" runat="server"
       EnableViewState="False"
       CellPadding="5"
       GridLines="None"
       HeaderStyle-BackColor="silver"
       HeaderStyle-HorizontalAlign="center"
       FooterStyle-BackColor="silver"
       ShowFooter="True"
       AllowSorting="True"
       OnSortCommand="SortRows" />

</form>

<!--------------------------------------------------------------------------->

<script language="C#" runat="server">

	string gstrSortOrder;			// to hold the sort order
	string gstrFindText;			// to hold the filter expression

	void Page_Load(Object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			// set the value to be used for the RowFilter on the DataView
			gstrFindText = "Title LIKE '*" + txtFindText.Text + "*'";
		}
		else
		{
			// set the default values for the sort string and filter text box
			gstrSortOrder = "ISBN";
			txtFindText.Text = "ASP";
		}

		// recreate the data set and bind to the DataGrid control
		BindDataGrid();
	}


	void SortRows(Object objSender, DataGridSortCommandEventArgs objArgs )
	{
		// runs when the column headings in the DataGrid are clicked

		// get the sort expression (name of the column heading that was clicked)
		gstrSortOrder = objArgs.SortExpression.ToString();

		// recreate the data set and bind to the DataGrid control
		BindDataGrid();
	}


	void BindDataGrid()
	{
		// get connection string from ..\global\connect-strings.ascx user control
		string strConnect = ctlConnectStrings.OLEDBConnectionString;

		// create a SQL statement to select some rows from the database
		string strSelect = "SELECT * FROM BookList";

		// create a variable to hold an instance of a DataView object
		DataView objDataView;

		// get DataView from get-dataview-control.ascx user control
		objDataView = ctlDataView.GetDataView(strConnect, strSelect);

		if (objDataView == null)
			return;

		// sort the rows in the DataView into the specified order
		objDataView.Sort = gstrSortOrder;

		// select the rows in the DataView that match the filter
		objDataView.RowFilter = gstrFindText;

		// set the DataSource property of the DataList
		MyDataGrid.DataSource = objDataView;

		// and bind the control to the data
		MyDataGrid.DataBind();
	}

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

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