我有以下ListView应该引发一个ItemInserting事件,但是当我逐步完成这个过程时,它永远不会遇到这个事件。有线索吗?找到所有FindChild控件并返回值。我已经搜索了几个小时,但不明白为什么它不起作用。使用ASP.Net 4.0。
<asp:SqlDataSource ID="srcSiteInvite" ConnectionString="<%$ ConnectionStrings:csRemote %>"
SelectCommand="spSiteInvite" SelectCommandType="StoredProcedure"
InsertCommand="INSERT INTO Friends(RequestSenderUserID, FriendFirstName, FriendLastName, FriendEmail) VALUES ( @User , @FirstName, @LastName, @Email)"
runat="server">
<InsertParameters>
<asp:Parameter Name="User" />
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="Email" />
</InsertParameters>
</asp:SqlDataSource>
<asp:ListView ID="lsvSiteInvite" InsertItemPosition="FirstItem" DataSourceID="srcSiteInvite" OnItemInserting="lsvSiteInvite_ItemInserting" runat="server">
<InsertItemTemplate>
<tr>
<td>
<h3>Enter Friend Information</h3>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendFirstName" Text="<b>First Name:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendFirstName" Text='<%# Bind("FriendFirstName") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendFirstName" ControlToValidate="txtFriendFirstName"
ErrorMessage="Friend first name is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendLastName" Text="<b>Last Name:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendLastName" Text='<%# Bind("FriendLastName") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendLastName" ControlToValidate="txtFriendLastName"
ErrorMessage="Friend last name is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendEmail" Text="<b>E-mail:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendEmail" Text='<%# Bind("FriendEmail") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendEmail" ControlToValidate="txtFriendEmail"
ErrorMessage="Friend e-mail is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFriendEmail" ControlToValidate="txtFriendEmail"
ErrorMessage="E-mail is not in proper format." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnFriendSubmit" CommandArgument="Insert" Text="Submit" runat="server" />
<asp:Button ID="btnFriendCancel" CommandArgument="Cancel" Text="Cancel" OnClick="btnFriendCancel_Click" runat="server" />
</td>
</tr>
</InsertItemTemplate>
...
</asp:ListView>
protected void lsvSiteInvite_ItemInserting(Object sender, ListViewInsertEventArgs e)
{
TextBox txt = Helpers.FindChild(this, "txtFriendFirstName") as TextBox;
e.Values["FirstName"]=txt.Text;
txt = Helpers.FindChild(this, "txtFriendLastName") as TextBox;
e.Values["LastName"] = txt.Text;
txt = Helpers.FindChild(this, "txtFriendEmail") as TextBox;
e.Values["Email"] = txt.Text;
e.Values["User"] = UserInformation.UserID;
}
答案 0 :(得分:2)
您需要将OnItemInserting="lsvSiteInvite_ItemInserting"
添加到listview标记中。这会将方法绑定到OnItemInserting
事件:
编辑2:您需要将CommandName传递给ListView。 更改此行:
<asp:Button ID="btnFriendSubmit" CommandArgument="Insert" Text="Submit" runat="server" />
到此:
<asp:Button ID="btnFriendSubmit" CommandName ="Insert" Text="Submit" runat="server" />
编辑:在您的标记中,您有一个带有“插入”命令的按钮。看起来你期望触发ListView的OnItemInserting事件。要使用该按钮,您必须将OnItemCommand="lsvSiteInvite_ItemCommand"
添加到列表视图标记中,并在代码lsvSiteInvite_ItemCommand
方法中调用:
<asp:ListView ID="lsvSiteInvite" InsertItemPosition="FirstItem"
DataSourceID="srcSiteInvite" OnItemCommand="lsvSiteInvite_ItemCommand" runat="server">
编辑3:我使用以下代码和标记来测试您的代码。您可以下载我的测试项目和DbScript here。
<强> WebForm1.aspx的强>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ListViewTest.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="srcSiteInvite" ConnectionString="<%$ ConnectionStrings:csRemote %>"
SelectCommand="spSiteInvite" SelectCommandType="StoredProcedure"
InsertCommand="INSERT INTO Friends(RequestSenderUserID, FriendFirstName, FriendLastName, FriendEmail) VALUES ( @User , @FirstName, @LastName, @Email)"
runat="server">
<InsertParameters>
<asp:Parameter Name="User" />
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="Email" />
</InsertParameters>
</asp:SqlDataSource>
<asp:ListView ID="lsvSiteInvite" InsertItemPosition="FirstItem"
DataSourceID="srcSiteInvite" OnItemCommand="lsvSiteInvite_ItemCommand" runat="server">
<InsertItemTemplate>
<tr>
<td>
<h3>Enter Friend Information</h3>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendFirstName" Text="<b>First Name:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendFirstName" Text='<%# Bind("FriendFirstName") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendFirstName" ControlToValidate="txtFriendFirstName"
ErrorMessage="Friend first name is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendLastName" Text="<b>Last Name:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendLastName" Text='<%# Bind("FriendLastName") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendLastName" ControlToValidate="txtFriendLastName"
ErrorMessage="Friend last name is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="lblFriendEmail" Text="<b>E-mail:</b>" Width="85px" runat="server" />
<asp:TextBox ID="txtFriendEmail" Text='<%# Bind("FriendEmail") %>' runat="server" />
<asp:RequiredFieldValidator ID="rfvFriendEmail" ControlToValidate="txtFriendEmail"
ErrorMessage="Friend e-mail is required." runat="server"><span style="font-weight:bold; color:Red;">*</span>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFriendEmail" ControlToValidate="txtFriendEmail"
ErrorMessage="E-mail is not in proper format." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnFriendSubmit" CommandName="Insert" Text="Submit" runat="server" />
<asp:Button ID="btnFriendCancel" CommandArgument="Cancel" Text="Cancel" OnClick="btnFriendCancel_Click" runat="server" />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<br />
<table>
<tr><td>
<asp:Label ID="Label1" Text="<b>First Name:</b>" Width="85px" runat="server" /></td><td>
<asp:Label ID="lblFriendFirstName" Text='<%# Bind("FriendFirstName") %>' Width="85px" runat="server" /><br /></td></tr>
<tr><td>
<asp:Label ID="Label2" Text="<b>Last Name:</b>" Width="85px" runat="server" /></td><td>
<asp:Label ID="lblFriendLastName" Text='<%# Bind("FriendLastName") %>' Width="85px" runat="server" /><br /></td></tr>
<tr><td>
<asp:Label ID="Label3" Text="<b>E-mail:</b>" Width="85px" runat="server" /></td><td>
<asp:Label ID="lblFriendEmail" Text='<%# Bind("FriendEmail") %>' Width="85px" runat="server" /></td></tr>
</table>
<br />
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
<强> WebForm1.ascx.cs:强>
using System;
using System.Web.UI.WebControls;
namespace ListViewTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lsvSiteInvite_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
ListViewItem item = (ListViewItem)e.Item;
var firstName = item.FindControl("txtFriendFirstName") as TextBox;
var lastName = item.FindControl("txtFriendLastName") as TextBox;
var email = item.FindControl("txtFriendEmail") as TextBox;
var user = "1";// UserInformation.UserID;//Test code
if (firstName != null && lastName != null && email != null)
{
//Your code to insert
srcSiteInvite.InsertParameters["User"].DefaultValue = user;
srcSiteInvite.InsertParameters["FirstName"].DefaultValue = firstName.Text.ToString();
srcSiteInvite.InsertParameters["LastName"].DefaultValue = lastName.Text.ToString();
srcSiteInvite.InsertParameters["Email"].DefaultValue = email.Text.ToString();
}
}
}
protected void btnFriendCancel_Click(object sender, EventArgs e)
{
}
}
}
这是我从测试中得到的输出:
答案 1 :(得分:1)
这是有效的:
protected void lsvSiteInvite_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
ListViewItem item = (ListViewItem)e.Item;
var firstName = item.FindControl("txtFriendFirstName") as TextBox;
var lastName = item.FindControl("txtFriendLastName") as TextBox;
var email = item.FindControl("txtFriendEmail") as TextBox;
var userInfoList = UserInfo.UserInfoGetByUserID(UserID);
UserInformation = userInfoList.FirstOrDefault();
var user = UserInformation.UserID;
if (firstName != null && lastName != null && email != null)
{
//Your code to insert
srcSiteInvite.InsertParameters["User"].DefaultValue = user.ToString();
srcSiteInvite.InsertParameters["FirstName"].DefaultValue = firstName.Text.ToString();
srcSiteInvite.InsertParameters["LastName"].DefaultValue = lastName.Text.ToString();
srcSiteInvite.InsertParameters["Email"].DefaultValue = email.Text.ToString();
}
}
}
答案 2 :(得分:0)
对于插入项目您必须触发listview的OnItemInserting事件。为了这个只是列表视图的属性,双击OnItemInserting转到代码文件并在此方法中编写插入代码 就像html是
<asp:ListView ID="lsvSiteInvite" InsertItemPosition="FirstItem" OnItemInserting="lsvSiteInvite_ItemInserting" DataSourceID="srcSiteInvite" runat="server">
并在代码文件中
protected void lsvSiteInvite_ItemInserting(Object sender, ListViewInsertEventArgs e)
{
TextBox txt = Helpers.FindChild(this, "txtFriendFirstName") as TextBox;
e.Values["FirstName"]=txt.Text;
txt = Helpers.FindChild(this, "txtFriendLastName") as TextBox;
e.Values["LastName"] = txt.Text;
txt = Helpers.FindChild(this, "txtFriendEmail") as TextBox;
e.Values["Email"] = txt.Text;
e.Values["User"] = UserInformation.UserID;
}
请参阅here listview的项目插入事件此事件仅触发。如果单击具有命令名称Insert的控件