GridViewUpdateEventArgs.Keys 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
업데이트할 행의 기본 키를 나타내는 필드 이름/값 쌍의 사전을 가져옵니다.
public:
property System::Collections::Specialized::IOrderedDictionary ^ Keys { System::Collections::Specialized::IOrderedDictionary ^ get(); };
public System.Collections.Specialized.IOrderedDictionary Keys { get; }
member this.Keys : System.Collections.Specialized.IOrderedDictionary
Public ReadOnly Property Keys As IOrderedDictionary
속성 값
IOrderedDictionary 업데이트할 행의 기본 키를 나타내는 필드 이름/값 쌍을 포함하는 개체입니다.
예제
다음 예제에서는 속성을 사용하여 Keys 업데이트할 행의 키 필드 값에 액세스하는 방법을 보여 줍니다. 그런 다음 값이 업데이트된 레코드의 로그 파일에 기록됩니다.
<%@ Page language="C#" %>
<%@ import namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void EmployeesGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Record the update operation in a log file.
// Create the log text.
String logText = "";
// Append the key field values to the log text.
foreach (DictionaryEntry keyEntry in e.Keys)
{
logText += keyEntry.Key + "=" + keyEntry.Value + ";";
}
// Append the text to a log file.
StreamWriter sw;
sw = File.AppendText(Server.MapPath(null) + "\\updatelog.txt");
sw.WriteLine(logText);
sw.Flush();
sw.Close();
}
void EmployeesGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception == null)
{
// The update operation succeeded. Clear the message label.
Message.Text = "";
}
else
{
// The update operation failed. Display an error message.
Message.Text = e.AffectedRows.ToString() + " rows updated. " + e.Exception.Message;
e.ExceptionHandled = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridViewUpdateEventArgs Keys Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>GridViewUpdateEventArgs Keys Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames attribute as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="EmployeesGridView"
datasourceid="EmployeesSqlDataSource"
DataKeyNames="EmployeeID"
autogenerateeditbutton="True"
onrowupdating="EmployeesGridView_RowUpdating"
onrowupdated="EmployeesGridView_RowUpdated"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="EmployeesSqlDataSource"
selectcommand="SELECT [EmployeeID], [LastName], [FirstName], [HireDate] FROM [Employees]"
updatecommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server" >
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<%@ import namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub EmployeesGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
' Record the update operation in a log file.
' Create the log text.
Dim logText As String = ""
' Append the key field values to the log text.
Dim keyEntry As DictionaryEntry
For Each keyEntry In e.Keys
logText += keyEntry.Key & "=" & keyEntry.Value & ";"
Next keyEntry
' Append the text to a log file.
Dim sw As StreamWriter
sw = File.AppendText(Server.MapPath(Nothing) & "\updatelog.txt")
sw.WriteLine(logText)
sw.Flush()
sw.Close()
End Sub
Sub EmployeesGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
If e.Exception Is Nothing Then
' The update operation succeeded. Clear the message label.
Message.Text = ""
Else
' The update operation failed. Display an error message.
Message.Text = e.AffectedRows.ToString() & " rows updated. " & e.Exception.Message
e.ExceptionHandled = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewUpdateEventArgs Keys Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>GridViewUpdateEventArgs Keys Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames attribute as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="EmployeesGridView"
datasourceid="EmployeesSqlDataSource"
DataKeyNames="EmployeeID"
autogenerateeditbutton="True"
onrowupdating="EmployeesGridView_RowUpdating"
onrowupdated="EmployeesGridView_RowUpdated"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="EmployeesSqlDataSource"
selectcommand="SELECT [EmployeeID], [LastName], [FirstName], [HireDate] FROM [Employees]"
updatecommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server" >
</asp:sqldatasource>
</form>
</body>
</html>
설명
컨트롤의 DataKeyNamesGridView 속성이 설정되면 속성(사전)을 사용하여 Keys 업데이트할 행의 기본 키 값에 액세스합니다.
메모
키가 아닌 필드의 값에 액세스하려면 해당 또는 OldValues 속성을 사용합니다NewValues. 속성에는 NewValues 업데이트된 값이 OldValues 포함되지만 속성에는 원래 값이 포함됩니다.
사전은 Keys 속성에 지정된 DataKeyNames 필드 또는 필드의 이름/값 쌍으로 자동으로 채워집니다. 여러 필드가 기본 키를 형성하는 경우 각 키 필드의 Keys 사전에 별도의 항목이 추가됩니다.
키 필드의 이름을 확인하려면 사전에 포함된 Keys 개체의 System.Collections.DictionaryEntry 속성을 사용합니다DictionaryEntry.Key. 키 필드의 값을 확인하려면 속성을 사용합니다 DictionaryEntry.Value .