gridview

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx


.NET Framework Class Library
GridView..::.RowUpdating Event

Occurs when a row's Update button is clicked, but before the GridView control updates the row.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Visual Basic (Declaration)
Public Event RowUpdating As GridViewUpdateEventHandler
Visual Basic (Usage)
Dim instance As GridView
Dim handler As GridViewUpdateEventHandler
AddHandler instance.RowUpdating, handler
C#
public event GridViewUpdateEventHandler RowUpdating
Visual C++
public:
event GridViewUpdateEventHandler^ RowUpdating {
void add (GridViewUpdateEventHandler^ value);
void remove (GridViewUpdateEventHandler^ value);
}
J#
/** @event */
public void add_RowUpdating (GridViewUpdateEventHandler value)
/** @event */
public void remove_RowUpdating (GridViewUpdateEventHandler value)
JScript
JScript does not support events.
ASP.NET
<asp:GridView OnRowUpdating="GridViewUpdateEventHandler" />

The RowUpdating event is raised when a row's Update button is clicked, but before the GridView control updates the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the update operation, whenever this event occurs.

A GridViewUpdateEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the update operation should be canceled. To cancel the update operation, set the Cancel property of the GridViewUpdateEventArgs object to true. You can also manipulate the Keys, OldValues, and NewValues collections, if necessary, before the values are passed to the data source. A common way to use these collections is to HTML-encode the values supplied by the user before they are stored in the data source. This helps to prevent script injection attacks.

For more information about handling events, see Consuming Events.

The following example demonstrates how to use the RowUpdating event to HTML-encode all values supplied by the user before updating the data source.

Visual Basic
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
' Use the CopyTo method to copy the DictionaryEntry objects in the 
' NewValues collection to an array.
Dim records(e.NewValues.Count - 1) As DictionaryEntry
e.NewValues.CopyTo(records, 0)
' Iterate through the array and HTML encode all user-provided values 
' before updating the data source.
Dim entry As DictionaryEntry
For Each entry In records
e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())
Next
End Sub
</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>
<%@ 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>

The following example demonstrates how to use the RowUpdating event to update the values in the data source object when the data source is set programmatically.

Visual Basic
<%@ Page Language="VB" %>
<%@ 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 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
' Create a new table.
Dim taskTable As New DataTable("TaskList")
' Create the columns.
taskTable.Columns.Add("Id", GetType(Integer))
taskTable.Columns.Add("Description", GetType(String))
taskTable.Columns.Add("IsComplete", GetType(Boolean))
'Add data to the new table.
For i As Integer = 0 To 9
Dim tableRow As DataRow = taskTable.NewRow()
tableRow("Id") = i
tableRow("Description") = "Task " + i.ToString()
tableRow("IsComplete") = False
taskTable.Rows.Add(tableRow)
Next
'Persist the table in the Session object.
Session("TaskTable") = taskTable
'Bind data to the GridView control.
BindData()
End If
End Sub
Protected Sub TaskGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex
'Bind data to the GridView control.
BindData()
End Sub
Protected Sub TaskGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
'Reset the edit index.
TaskGridView.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
Protected Sub TaskGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'Retrieve the table from the session object.
Dim dt As DataTable = CType(Session("TaskTable"), DataTable)
'Update the values.
Dim row As GridViewRow = TaskGridView.Rows(e.RowIndex)
dt.Rows(TaskGridView.EditIndex)("Id") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
dt.Rows(TaskGridView.EditIndex)("Description") = (CType((row.Cells(2).Controls(0)), TextBox)).Text
dt.Rows(TaskGridView.EditIndex)("IsComplete") = (CType((row.Cells(3).Controls(0)), CheckBox)).Checked
'Reset the edit index.
TaskGridView.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
Private Sub BindData()
TaskGridView.DataSource = Session("TaskTable")
TaskGridView.DataBind()
End Sub
</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>
<%@ 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>
posted @ 2008-03-18 02:52  N/A2011  阅读(446)  评论(0编辑  收藏  举报