edit-rows.aspx

来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 130 行

ASPX
130
字号
<%@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>
<title>Editing Values in the Rows of a DataTable</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Editing Values in the Rows of a DataTable</span><hr />
<!--------------------------------------------------------------------------->

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

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server">&nbsp;</div>

<b>The original table contents</b>:
<asp:datagrid id="dgrResult1" runat="server" /><br />
<b>The table contents after editing Rows(0)</b>:
<asp:datagrid id="dgrResult2" runat="server" /><br />
<b>The table contents after calling CancelEdit()</b>:
<asp:datagrid id="dgrResult3" runat="server" /><br />
<b>The table contents after repeating the edit</b>:
<asp:datagrid id="dgrResult4" runat="server" /><br />
<b>The table contents after calling EndEdit()</b>:
<asp:datagrid id="dgrResult5" runat="server" /><br />

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

	void Page_Load(Object sender, EventArgs e)
	{

		// get connection string from ..\global\connect-strings.ascx user control
		string strConnect = ctlConnectStrings.OLEDBConnectionString;
		outConnect.InnerText = strConnect; // and display it

		// specify the SELECT statement to extract the BookList data
		string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861004%'";
		outSelect.InnerText = strSelect;   // and display it

		// create a new DataSet object
		DataSet objDataSet = new DataSet();

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

			// create a new DataAdapter object
			OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);

			// get the data from the "BookList" table in the database and
			// put it into a table named "Books" in the DataSet object
			objDataAdapter.Fill(objDataSet, "Books");
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accesing data</b>.<br />"
					+ objError.Message + "<br />" + objError.Source;
			return;  //  and stop execution
		}

		
		DataTable objTable = objDataSet.Tables["Books"];

		// assign the DataTable's DefaultView object to the DataGrid control
		dgrResult1.DataSource = objTable.DefaultView;
		dgrResult1.DataBind();  // and bind (display) the data

		// now edit the first row
		DataRow objRow = objTable.Rows[0];

		objRow.BeginEdit();

		// change some of the values in the row
		objRow["ISBN"] = "2000000000";
		objRow["Title"] = "Professional Video Recorder Programming";
		objRow["PublicationDate"] = new DateTime(2001, 3, 1);

		// then display the contents again
		dgrResult2.DataSource = objTable.DefaultView;
		dgrResult2.DataBind();

		// now we can check if the values are valid
		if (String.Compare(objRow["ISBN", DataRowVersion.Proposed].ToString(), "1999999999") > 0)
			objRow.CancelEdit();
		else
			objRow.EndEdit();

		// then display the contents again
		dgrResult3.DataSource = objTable.DefaultView;
		dgrResult3.DataBind();

		objRow.BeginEdit();
		// change some of the values in the row
		objRow["ISBN"] = "1234567800";
		objRow["Title"] = "Professional Video Recorder Programming";
		objRow["PublicationDate"] = new DateTime(200, 10, 10);

		// and display the contents again
		dgrResult4.DataSource = objTable.DefaultView;
		dgrResult4.DataBind();

		// now we can check if the values are valid
		if (String.Compare(objRow["ISBN", DataRowVersion.Proposed].ToString(), "1999999999") > 0)
			objRow.CancelEdit();
		else
			objRow.EndEdit();

		// and display the contents again
		dgrResult5.DataSource = objTable.DefaultView;
		dgrResult5.DataBind();

	}
</script>


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

⌨️ 快捷键说明

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