GridView.EditIndex 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置要编辑的行的索引。
public:
virtual property int EditIndex { int get(); void set(int value); };
public virtual int EditIndex { get; set; }
member this.EditIndex : int with get, set
Public Overridable Property EditIndex As Integer
属性值
要编辑的行的从零开始的索引。 默认值为 -1,指示没有正在编辑的行。
例外
指定的索引小于 -1。
示例
下面的示例演示如何使用 EditIndex 属性来确定在 控件中 GridView 编辑后更新的行。 将显示一条消息,指示更新成功。
<%@ 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_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Clear the message label when the user enters edit mode.
if (e.CommandName == "Edit")
{
Message.Text = "";
}
}
void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
// The update operation was successful. Retrieve the row being edited.
int index = CustomersGridView.EditIndex;
GridViewRow row = CustomersGridView.Rows[index];
// Notify the user that the update was successful.
Message.Text = "Updated record " + row.Cells[1].Text + ".";
}
void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
// The update operation was canceled. Display the appropriate message.
Message.Text = "Update operation canceled.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Rows Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Rows Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- 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"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
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)"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ 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_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' Clear the message label when the user enters edit mode.
If e.CommandName = "Edit" Then
Message.Text = ""
End If
End Sub
Sub CustomersGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
' The update operation was successful. Retrieve the row being edited.
Dim index As Integer = CustomersGridView.EditIndex
Dim row As GridViewRow = CustomersGridView.Rows(index)
' Notify the user that the update was successful.
Message.Text = "Updated record " & row.Cells(1).Text + "."
End Sub
Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
' The update operation was canceled. Display the appropriate message.
Message.Text = "Update operation canceled."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Rows Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Rows Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- 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"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
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)"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
行索引从零开始, (第一行为) 行零。
此属性通常仅在以下方案中使用,其中涉及特定事件的处理程序:
希望控件在 GridView 首次显示页面时以编辑模式打开特定行。 为此,可以在 处理程序中为 Load 类或 控件的 PageGridView 事件设置 EditIndex 属性。
你想知道在更新行后编辑了哪个行。 为此,可以从事件处理程序中的 RowUpdated 属性检索行索引EditIndex。
通过以编程方式设置 属性,DataSource将控件绑定到GridView数据源。 在这种情况下,EditIndex必须在 和 RowCancelingEdit 事件处理程序中RowEditing设置 属性。
如果在回发之后或在事件之后Load引发的事件的处理程序中设置 EditIndex 属性,控件GridView可能不会进入指定行的编辑模式。 如果在其他事件处理程序中读取此属性的值,则不能保证索引反映正在编辑的行。
若要确定用户在控件进入编辑模式之前GridView单击了编辑按钮或超链接的行,可以从 事件处理程序中 RowEditing 对象的 属性GridViewEditEventArgs检索行索引NewEditIndex。
若要防止控件在GridView用户单击“编辑”按钮或超链接后进入编辑模式,请在 事件处理程序中RowEditing将 GridViewEditEventArgs 对象的 属性设置为 Canceltrue
。