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

📄 edit-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>Editing 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:9pt}
.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">Editing Data in a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

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

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

<ASP:Label id="lblSQL" runat="server" /><p />

<form runat="server">

  <ASP:DataGrid id="MyDataGrid" runat="server"
       CellPadding = "2"
       EditItemStyle-BackColor="yellow"
       DataKeyField="ISBN"
       OnEditCommand="DoItemEdit"
       OnUpdateCommand="DoItemUpdate"
       OnCancelCommand="DoItemCancel"
       AutoGenerateColumns="False">

    <Columns>

      <ASP:BoundColumn DataField="ISBN" HeaderText="ISBN" ReadOnly="True" />

      <ASP:TemplateColumn HeaderText="Title">
        <ItemTemplate>
          <ASP:Label Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
          <ASP:TextBox id="txtTitle" Size="60"
               Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
        </EditItemTemplate>
      </ASP:TemplateColumn>

      <ASP:BoundColumn DataField="PublicationDate" HeaderText="Published" />

      <ASP:EditCommandColumn
           EditText="Edit"
           CancelText="Cancel"
           UpdateText="Update" />

    </Columns>

  </ASP:DataGrid>

</form>

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

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

	void Page_Load(Object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
			BindDataGrid();		// create data set and bind to grid control
	}

	
	void DoItemEdit(Object objSource, DataGridCommandEventArgs objArgs)
	{
		lblSQL.Text = "";		// clear text from label that shows SQL statement

		// set the EditItemIndex property of the grid to this item's index
		MyDataGrid.EditItemIndex = objArgs.Item.ItemIndex;
		BindDataGrid();		// bind the data and display it
	}


	void DoItemUpdate(Object objSource, DataGridCommandEventArgs objArgs)
	{
		// get a reference to the title and publication date text boxes
		TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
		TextBox objPubDateCtrl = (TextBox)objArgs.Item.Cells[2].Controls[0];

		// create a suitable SQL statement and execute it
		string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
					+ "PublicationDate='" + objPubDateCtrl.Text + "' "
					+ "WHERE ISBN='" + MyDataGrid.DataKeys[objArgs.Item.ItemIndex] + "'";
		ExecuteSQLStatement(strSQL);

		// set EditItemIndex property of grid to -1 to switch out of Edit mode
		MyDataGrid.EditItemIndex = -1;
		BindDataGrid();		// bind the data and display it
	}


	void DoItemCancel(Object objSource, DataGridCommandEventArgs objArgs)
	{
		// set EditItemIndex property of grid to -1 to switch out of Edit mode
		MyDataGrid.EditItemIndex = -1;
		BindDataGrid();		// bind the data and display it
	}


	void ExecuteSQLStatement(string strSQL)
	{
		// this is where the SQL statement would be executed against the
		// original data source. In this example, we're simply displaying
		// the statement in a Label on the page
		lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
	}


	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 '%18610025%'";

		// 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 and bind the grid
		MyDataGrid.DataSource = objDataReader;
		MyDataGrid.DataBind();
	}

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
+copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>

⌨️ 快捷键说明

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