0

我制作了一个GridView带有删除和编辑链接的按钮来执行编辑和删除操作。这适用于编辑按钮,但不适用于删除按钮。问题是什么?

当我运行我的应用程序时,它给了我这个错误:

必须声明标量变量“@UserName”

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="SqlDataSource1">
<Columns>
<asp:CheckBoxField DataField="IsApproved" HeaderText="IsApproved"
SortExpression="IsApproved" />
<asp:BoundField DataField="UserName" HeaderText="UserName" 
SortExpression="UserName" />
<asp:CheckBoxField DataField="IsLockedOut" HeaderText="IsLockedOut" 
SortExpression="IsLockedOut" />
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
SelectCommand="SELECT [IsApproved], [UserName], [IsLockedOut] FROM [vw_aspnet_MembershipUsers]" 
UpdateCommand="UPDATE [vw_aspnet_MembershipUsers] SET IsApproved = @IsApproved, IsLockedOut = @IsLockedOut WHERE (UserName = @UserName)"
DeleteCommand="DELETE FROM [vw_aspnet_MembershipUsers] WHERE (UserName = @UserName)">
<UpdateParameters>
<asp:Parameter Name="IsApproved" />
<asp:Parameter Name="IsLockedOut" />
<asp:Parameter Name="UserName" />
</UpdateParameters> 
<DeleteParameters>
<asp:Parameter Name="IsApproved" />
<asp:Parameter Name="IsLockedOut" />
<asp:Parameter Name="UserName" />
</DeleteParameters> 

</asp:SqlDataSource>
</asp:Content>
4

2 回答 2

2

您应该通过Membership.DeleteUser(user.UserName)或通过存储过程删除[dbo].[aspnet_Users_DeleteUser]

http://www.salmontraining.com/SqlExpressZone/articles/deleting_users_from_membership.html

于 2012-05-26T00:53:14.043 回答
0

我的建议是处理 SQLDataSource 的“删除”事件。调用 asp.net 会员 api 从后面的代码中删除。

确保导入 System.Web.Security,然后尝试以下操作:

    // Delete Membership User 
    Membership.DeleteUser(UserName);

无论如何,从视图中删除记录并不是最好的方法。

于 2012-05-26T00:52:38.927 回答