DetailsViewInsertEventArgs.Values 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取包含要插入的记录所对应的字段名/值对的字典。
public:
property System::Collections::Specialized::IOrderedDictionary ^ Values { System::Collections::Specialized::IOrderedDictionary ^ get(); };
public System.Collections.Specialized.IOrderedDictionary Values { get; }
member this.Values : System.Collections.Specialized.IOrderedDictionary
Public ReadOnly Property Values As IOrderedDictionary
属性值
一个 IOrderedDictionary,包含要插入的记录所对应的键字段名称/值对的字典。
示例
下面的代码示例演示如何使用 Values 属性访问要插入的记录的字段值。
<%@ 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 CustomerDetailsView_ItemInserting(Object sender,
DetailsViewInsertEventArgs e)
{
// Use the Values property to retrieve the key field value.
String keyValue = e.Values["CustomerID"].ToString();
// Insert the record only if the key field is four characters
// long; otherwise, cancel the insert operation.
if (keyValue.Length == 4)
{
// Change the key field value to upper case before inserting
// the record in the data source.
e.Values["CustomerID"] = keyValue.ToUpper();
MessageLabel.Text = "";
}
else
{
MessageLabel.Text = "The key field must have four digits.";
e.Cancel = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventArgs Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventArgs Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
oniteminserting="CustomerDetailsView_ItemInserting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode],
[Country]) VALUES (@CustomerID, @CompanyName, @Address,
@City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" autoeventwireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerDetailsView_ItemInserting(ByVal sender As Object, _
ByVal e As DetailsViewInsertEventArgs) _
Handles CustomerDetailsView.ItemInserting
' Use the Values property to retrieve the key field value.
Dim keyValue As String = e.Values("CustomerID").ToString()
' Insert the record only if the key field is four characters
' long; otherwise, cancel the insert operation.
If keyValue.Length = 4 Then
' Change the key field value to upper case before inserting
' the record in the data source.
e.Values("CustomerID") = keyValue.ToUpper()
MessageLabel.Text = ""
Else
MessageLabel.Text = "The key field must have four digits."
e.Cancel = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventArgs Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventArgs Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
oniteminserting="CustomerDetailsView_ItemInserting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode],
[Country]) VALUES (@CustomerID, @CompanyName, @Address,
@City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
Values使用 属性访问要插入的记录的字段的值。 例如,可以在将记录的值插入数据源之前对记录的值进行 HTML 编码。
Values 属性返回一个实现 IOrderedDictionary 接口的对象。 对象包含 DictionaryEntry 表示记录字段的对象。
注意
作为快捷方式,还可以使用 对象的索引器 IOrderedDictionary 直接访问字段值。