ListView.InsertNewItem(Boolean) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前记录插入到数据源中。
public:
virtual void InsertNewItem(bool causesValidation);
public virtual void InsertNewItem (bool causesValidation);
abstract member InsertNewItem : bool -> unit
override this.InsertNewItem : bool -> unit
Public Overridable Sub InsertNewItem (causesValidation As Boolean)
参数
- causesValidation
- Boolean
true
表示在调用此方法时执行页面验证;否则为 false
。
例外
示例
以下示例演示如何使用 InsertNewItem 方法以编程方式将控件的插入项 ListView 的内容插入数据源。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 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">
protected void InsertButton_Click(object sender, EventArgs e)
{
// Clears any existing message.
MessageLabel.Text = "";
// Use the InsertNewItem method to programmatically insert
// the current record in the ListView control.
DepartmentsListView.InsertNewItem(true);
}
protected void DepartmentsListView_ItemInserted(object sender,
ListViewInsertedEventArgs e)
{
// Handles exceptions that might occur
// during the insert operation.
if (e.Exception != null)
{
if (e.AffectedRows == 0)
{
e.KeepInInsertMode = true;
MessageLabel.Text = "An exception occurred inserting the new department. " +
"Please verify your values and try again.";
}
else
MessageLabel.Text = "An exception occurred inserting the new department. " +
"Please verify the values in the newly inserted item.";
e.ExceptionHandled = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView InsertNewItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView InsertNewItem Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
InsertItemPosition="FirstItem"
OnItemInserted="DepartmentsListView_ItemInserted"
runat="server" >
<LayoutTemplate>
<table runat="server" id="tblDepartments" width="640px" border="1">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="NameLabel" runat="Server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="Server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#00BFFF">
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name:" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name:" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Text='<%#Bind("GroupName") %>' MaxLength="50" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView><br />
<asp:Label ID="MessageLabel"
ForeColor="Red"
runat="server" /> <br />
<asp:Button ID="InsertButton"
Text="Insert new record"
OnClick="InsertButton_Click"
runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</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">
Protected Sub InsertButton_Click(ByVal sender As Object, _
ByVal e As EventArgs)
' Clears any existing message.
MessageLabel.Text = ""
' Use the InsertNewItem method to programmatically insert
' the current record in the ListView control.
DepartmentsListView.InsertNewItem(True)
End Sub
Protected Sub DepartmentsListView_ItemInserted(ByVal sender As Object, _
ByVal e As ListViewInsertedEventArgs)
' Handles exceptions that might occur
' during the insert operation.
If Not (e.Exception Is Nothing) Then
If e.AffectedRows = 0 Then
e.KeepInInsertMode = True
MessageLabel.Text = "An exception occurred inserting the new department. " & _
"Please verify your values and try again."
Else
MessageLabel.Text = "An exception occurred inserting the new department. " & _
"Please verify the values in the newly inserted item."
End If
e.ExceptionHandled = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView InsertNewItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView InsertNewItem Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
InsertItemPosition="FirstItem"
OnItemInserted="DepartmentsListView_ItemInserted"
runat="server" >
<LayoutTemplate>
<table runat="server" id="tblDepartments" width="640px" border="1">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="NameLabel" runat="Server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="Server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#00BFFF">
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name:" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name:" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Text='<%#Bind("GroupName") %>' MaxLength="50" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView><br />
<asp:Label ID="MessageLabel"
ForeColor="Red"
runat="server" /> <br />
<asp:Button ID="InsertButton"
Text="Insert new record"
OnClick="InsertButton_Click"
runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</asp:SqlDataSource>
</form>
</body>
</html>
注解
InsertNewItem使用 方法以编程方式在数据源中插入项。 此方法通常用于从控件外部 ListView 插入项,例如从页面上的其他控件插入项。
若要使用 InsertNewItem 方法,必须在 控件中ListView定义模板InsertItemTemplate。 还必须将 InsertItemPosition 属性设置为不同于 InsertItemPosition.None的值。 若要指定是否在插入操作之前执行页面验证,请使用 causesValidation
参数。
此方法引发 ItemInserted 和 ItemInserting 事件。