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

📄 columns-datagrid.aspx

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

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Specifying the Columns 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">Specifying the Columns in a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

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

<div id="outError" runat="server" />

<form runat="server">

  'Release Date' Column:
  <ASP:RadioButton id="chkVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Visible &nbsp;
  <ASP:RadioButton id="chkNotVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Hidden<p />

  <ASP:DataGrid id="MyDataGrid" runat="server"
       AutoGenerateColumns="False"
       CellPadding="5"
       GridLines="None"
       HeaderStyle-BackColor="silver"
       HeaderStyle-HorizontalAlign="center"
       FooterStyle-BackColor="silver"
       ShowFooter="True"
       OnItemCommand="ShowInfo">
    <Columns>
      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="silver">
        <ItemTemplate>&nbsp;</ItemTemplate>
      </ASP:TemplateColumn>
      <ASP:BoundColumn HeaderText="<b>Code</b>" DataField="ISBN" ItemStyle-BackColor="lightblue" />
      <ASP:BoundColumn HeaderText="<b>Book Title</b>" DataField="Title"/>
      <ASP:BoundColumn HeaderText="<b>Released</b>" DataField="PublicationDate" DataFormatString="{0:D}" ItemStyle-HorizontalAlign="right" ItemStyle-BackColor="yellow" />
      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="lightblue">
        <ItemTemplate>
          <ASP:Button id="cmdInfo" Text="More Info" CommandName="Info" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
      <ASP:TemplateColumn HeaderText="<b>Buy Now</b>" ItemStyle-BackColor="silver" ItemStyle-HorizontalAlign="center">
        <ItemTemplate>
          <ASP:CheckBox id="chkBuy" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
    </Columns>
  </ASP:DataGrid><p />

  <ASP:Label id="lblInfo" runat="server" /><br />

</form>

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

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

	void Page_Load(Object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			// display or hide the "Release Date" column
			MyDataGrid.Columns[3].Visible = (chkVisible.Checked == true);
		}
		else
		{
			chkVisible.Checked = true;		// set default value
			BindDataGrid();					// create data set and bind grid
		}
	}


	void ShowInfo(Object objSender, DataGridCommandEventArgs objArgs )
	{
		// runs when any command button in the grid is clicked
		// see if the CommandName of the clicked button was "Info"
		if (objArgs.CommandName == "Info")
		{
			// get values of ISBN and Title from Text property of the table cells
			// for the current row returned in the objArgs parameter values
			string strISBN = objArgs.Item.Cells[1].Text;
			string strTitle = objArgs.Item.Cells[2].Text;

			// display the informaion in the page - possibly extract from database?
			lblInfo.Text = "More information about the book:<br /><b>" + strTitle
							+ "</b><br />(ISBN " + strISBN + ") goes here...";
		}
	}


	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 WHERE ISBN LIKE '%18610023%'";

		// create a variable to hold an instance of a DataReader object
		OleDbDataReader objDataReader;

		try
		{
			// create a new Connection object using the connection string
			OleDbConnection objConnect = new OleDbConnection(strConnect);

			// open the connection to the database
			objConnect.Open();

			// create a new Command using the connection object and select statement
			OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

			// execute the SQL statement against the command to get the DataReader
			objDataReader = objCommand.ExecuteReader();
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
								+ objError.Message + "<br />" + objError.Source + "<p />";
			return;		// and stop execution
		}

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

		// 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 + -