下面的示例演示如何使用 RowUpdating 事件在更新数据源之前对用户提供的所有值进行 HTML 编码。
C#
<%@ Page language=
"C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat= "server">
void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Iterate through the NewValues collection and HTML encode all
// user-provided values before updating the data source.
foreach (DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}
}
</script>
<html >
<head runat= "server">
<title>GridView RowUpdating Example</title>
</head>
<body>
<form id= "form1" runat= "server">
<h3>GridView RowUpdating Example</h3>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id= "CustomersGridView"
datasourceid= "CustomersSqlDataSource"
autogeneratecolumns= "true"
autogenerateeditbutton= "true"
allowpaging= "true"
datakeynames= "CustomerID"
onrowupdating= "CustomersGridView_RowUpdating"
runat= "server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id= "CustomersSqlDataSource"
selectcommand= "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand= "Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>"
runat= "server">
</asp:sqldatasource>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat= "server">
void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Iterate through the NewValues collection and HTML encode all
// user-provided values before updating the data source.
foreach (DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
}
}
</script>
<html >
<head runat= "server">
<title>GridView RowUpdating Example</title>
</head>
<body>
<form id= "form1" runat= "server">
<h3>GridView RowUpdating Example</h3>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id= "CustomersGridView"
datasourceid= "CustomersSqlDataSource"
autogeneratecolumns= "true"
autogenerateeditbutton= "true"
allowpaging= "true"
datakeynames= "CustomerID"
onrowupdating= "CustomersGridView_RowUpdating"
runat= "server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id= "CustomersSqlDataSource"
selectcommand= "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand= "Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>"
runat= "server">
</asp:sqldatasource>
</form>
</body>
</html>
下面的示例演示如何使用 RowUpdating 事件在以编程方式设置数据源时更新数据源对象中的值。
C#
<%@ Page Language=
"C#" %>
<%@ Import Namespace= "System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat= "server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Create a new table.
DataTable taskTable = new DataTable( "TaskList");
// Create the columns.
taskTable.Columns.Add( "Id", typeof( int));
taskTable.Columns.Add( "Description", typeof( string));
taskTable.Columns.Add( "IsComplete", typeof( bool) );
//Add data to the new table.
for ( int i = 0; i < 10; i++)
{
DataRow tableRow = taskTable.NewRow();
tableRow[ "Id"] = i;
tableRow[ "Description"] = "Task " + i.ToString();
tableRow[ "IsComplete"] = false;
taskTable.Rows.Add(tableRow);
}
//Persist the table in the Session object.
Session[ "TaskTable"] = taskTable;
//Bind data to the GridView control.
BindData();
}
}
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session[ "TaskTable"];
//Update the values.
GridViewRow row = TaskGridView.Rows[e.RowIndex];
dt.Rows[TaskGridView.EditIndex][ "Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[TaskGridView.EditIndex][ "Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[TaskGridView.EditIndex][ "IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
TaskGridView.DataSource = Session[ "TaskTable"];
TaskGridView.DataBind();
}
</script>
<html >
<head runat= "server">
<title>GridView example</title>
</head>
<body>
<form id= "form1" runat= "server">
<div>
<asp:GridView ID= "TaskGridView" runat= "server"
AutoGenerateEditButton= "True"
OnRowEditing= "TaskGridView_RowEditing"
OnRowCancelingEdit= "TaskGridView_RowCancelingEdit"
OnRowUpdating= "TaskGridView_RowUpdating">
</asp:GridView>
</div>
</form>
</body>
</html>
<%@ Import Namespace= "System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat= "server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Create a new table.
DataTable taskTable = new DataTable( "TaskList");
// Create the columns.
taskTable.Columns.Add( "Id", typeof( int));
taskTable.Columns.Add( "Description", typeof( string));
taskTable.Columns.Add( "IsComplete", typeof( bool) );
//Add data to the new table.
for ( int i = 0; i < 10; i++)
{
DataRow tableRow = taskTable.NewRow();
tableRow[ "Id"] = i;
tableRow[ "Description"] = "Task " + i.ToString();
tableRow[ "IsComplete"] = false;
taskTable.Rows.Add(tableRow);
}
//Persist the table in the Session object.
Session[ "TaskTable"] = taskTable;
//Bind data to the GridView control.
BindData();
}
}
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session[ "TaskTable"];
//Update the values.
GridViewRow row = TaskGridView.Rows[e.RowIndex];
dt.Rows[TaskGridView.EditIndex][ "Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[TaskGridView.EditIndex][ "Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[TaskGridView.EditIndex][ "IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
//Reset the edit index.
TaskGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
TaskGridView.DataSource = Session[ "TaskTable"];
TaskGridView.DataBind();
}
</script>
<html >
<head runat= "server">
<title>GridView example</title>
</head>
<body>
<form id= "form1" runat= "server">
<div>
<asp:GridView ID= "TaskGridView" runat= "server"
AutoGenerateEditButton= "True"
OnRowEditing= "TaskGridView_RowEditing"
OnRowCancelingEdit= "TaskGridView_RowCancelingEdit"
OnRowUpdating= "TaskGridView_RowUpdating">
</asp:GridView>
</div>
</form>
</body>
</html>