使用 sqlDataSource 从表和 ASPNET 成员资格提供程序中删除用户
我有一个 Gridview,它通过 sqlDataSource 显示联系人列表。
包括一个用于删除记录的项目模板 - 一个 LinkButton - 将 OnClientClick 设置为使用一些 JavaScript:“返回确认('您确定要删除此用户吗?');” 这会向用户弹出一个带有“是”或“取消”选项的小窗口。
从 Contact 表中删除用户时,我还需要使用 Membership.DeleteUser(UserName, true) 语句删除 AspNet Membership 表中的同一用户。
Membership 表的 Contact.IntranetUserName 列和 UserName 包含相同的数据值,这样就方便了。
不知道要使用什么事件,我开始使用 Gridview 的 OnRowCommand - 但无法弄清楚如何获取正在删除的行的“IntranetUserName”列的值(通过联系人表中的项目模板)并将其传递到 Membership.DeleteUser 语句。
任何关于此的C#想法都将受到高度赞赏。 也许我应该使用 GridView 的 OnRowDeleted 或 OnRowDeleting 来代替? 或者也许是 LinkButtons OnCommand?
GridView 和 SqlDataSource 在这里:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ContactID"
DataSourceID="SqlDataSource2" Width="738px" OnRowCommand="deleteFromAspnetProvider" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirm('Are you sure you want to delete this user?');"
CommandName="Delete" Text="Delete User"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="IntranetUserName" HeaderText="IntranetUserName" SortExpression="IntranetUserName" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" />
<asp:BoundField DataField="CompanyName" HeaderText="Company"
SortExpression="CompanyName" />
<asp:BoundField DataField="EmailAddress" HeaderText="Email Address"
SortExpression="EmailAddress" />
<asp:BoundField DataField="UserLevelType" HeaderText="UserLevelType"
SortExpression="UserLevelType" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:DataConnect %>"
SelectCommand="SELECT * FROM [Contact] WHERE ([CompanyName] = @CompanyName)"
DeleteCommand="DELETE FROM [Contact] WHERE [ContactID] = @ContactID"
InsertCommand="INSERT INTO [Contact] ([UserId], [UserSID], [LocationID], [IntranetUserName], [FirstName], [LastName], [CompanyName], [EmailAddress], [ContactPhone], [ContactPhoneExt], [ContactFAX], [DateUpdated], [ActiveMember], [UserLevelType]) VALUES (@UserId, @UserSID, @LocationID, @IntranetUserName, @FirstName, @LastName, @CompanyName, @EmailAddress, @ContactPhone, @ContactPhoneExt, @ContactFAX, @DateUpdated, @ActiveMember, @UserLevelType)"
UpdateCommand="UPDATE [Contact] SET [UserId] = @UserId, [UserSID] = @UserSID, [LocationID] = @LocationID, [IntranetUserName] = @IntranetUserName, [FirstName] = @FirstName, [LastName] = @LastName, [CompanyName] = @CompanyName, [EmailAddress] = @EmailAddress, [ContactPhone] = @ContactPhone, [ContactPhoneExt] = @ContactPhoneExt, [ContactFAX] = @ContactFAX, [DateUpdated] = @DateUpdated, [ActiveMember] = @ActiveMember, [UserLevelType] = @UserLevelType WHERE [ContactID] = @ContactID">
<SelectParameters>
<asp:ControlParameter ControlID="memberCompanyNameLabel" Name="CompanyName"
PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ContactID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="Object" />
<asp:Parameter Name="UserSID" Type="Int32" />
<asp:Parameter Name="LocationID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="EmailAddress" Type="String" />
<asp:Parameter Name="ContactPhone" Type="String" />
<asp:Parameter Name="ContactPhoneExt" Type="String" />
<asp:Parameter Name="ContactFAX" Type="String" />
<asp:Parameter Name="DateUpdated" Type="DateTime" />
<asp:Parameter Name="ActiveMember" Type="Boolean" />
<asp:Parameter Name="UserLevelType" Type="Int32" />
<asp:Parameter Name="ContactID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="UserId" Type="Object" />
<asp:Parameter Name="UserSID" Type="Int32" />
<asp:Parameter Name="LocationID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="EmailAddress" Type="String" />
<asp:Parameter Name="ContactPhone" Type="String" />
<asp:Parameter Name="ContactPhoneExt" Type="String" />
<asp:Parameter Name="ContactFAX" Type="String" />
<asp:Parameter Name="DateUpdated" Type="DateTime" />
<asp:Parameter Name="ActiveMember" Type="Boolean" />
<asp:Parameter Name="UserLevelType" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
I have a Gridview that shows a list of Contacts via a sqlDataSource.
Included is an Item Template for Deleting a record - a LinkButton - with OnClientClick set to use some JavaScript: "return confirm('Are you sure you want to delete this user?');" This give the user a little pop up with a Yes or Cancel option.
When the user is deleted from the Contact table, I also need to delete the same user in the AspNet Membership tables using Membership.DeleteUser(UserName, true) statement.
The Contact.IntranetUserName column and UserName of the Membership tables contain the same data values, so that makes things convenient.
Not knowing what event to use, I started with using the Gridview's OnRowCommand - but can't figure out how to grab the value of the "IntranetUserName" column of the row that's being deleted (by the Item Template in Contact Table) and pass it to the Membership.DeleteUser statement.
Any C# ideas on this would be much apprecitated. Perhaps I should be using GridView's OnRowDeleted or OnRowDeleting instead? Or perhaps the LinkButtons OnCommand?
GridView and SqlDataSource here:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ContactID"
DataSourceID="SqlDataSource2" Width="738px" OnRowCommand="deleteFromAspnetProvider" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirm('Are you sure you want to delete this user?');"
CommandName="Delete" Text="Delete User"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="IntranetUserName" HeaderText="IntranetUserName" SortExpression="IntranetUserName" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" />
<asp:BoundField DataField="CompanyName" HeaderText="Company"
SortExpression="CompanyName" />
<asp:BoundField DataField="EmailAddress" HeaderText="Email Address"
SortExpression="EmailAddress" />
<asp:BoundField DataField="UserLevelType" HeaderText="UserLevelType"
SortExpression="UserLevelType" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:DataConnect %>"
SelectCommand="SELECT * FROM [Contact] WHERE ([CompanyName] = @CompanyName)"
DeleteCommand="DELETE FROM [Contact] WHERE [ContactID] = @ContactID"
InsertCommand="INSERT INTO [Contact] ([UserId], [UserSID], [LocationID], [IntranetUserName], [FirstName], [LastName], [CompanyName], [EmailAddress], [ContactPhone], [ContactPhoneExt], [ContactFAX], [DateUpdated], [ActiveMember], [UserLevelType]) VALUES (@UserId, @UserSID, @LocationID, @IntranetUserName, @FirstName, @LastName, @CompanyName, @EmailAddress, @ContactPhone, @ContactPhoneExt, @ContactFAX, @DateUpdated, @ActiveMember, @UserLevelType)"
UpdateCommand="UPDATE [Contact] SET [UserId] = @UserId, [UserSID] = @UserSID, [LocationID] = @LocationID, [IntranetUserName] = @IntranetUserName, [FirstName] = @FirstName, [LastName] = @LastName, [CompanyName] = @CompanyName, [EmailAddress] = @EmailAddress, [ContactPhone] = @ContactPhone, [ContactPhoneExt] = @ContactPhoneExt, [ContactFAX] = @ContactFAX, [DateUpdated] = @DateUpdated, [ActiveMember] = @ActiveMember, [UserLevelType] = @UserLevelType WHERE [ContactID] = @ContactID">
<SelectParameters>
<asp:ControlParameter ControlID="memberCompanyNameLabel" Name="CompanyName"
PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ContactID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="Object" />
<asp:Parameter Name="UserSID" Type="Int32" />
<asp:Parameter Name="LocationID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="EmailAddress" Type="String" />
<asp:Parameter Name="ContactPhone" Type="String" />
<asp:Parameter Name="ContactPhoneExt" Type="String" />
<asp:Parameter Name="ContactFAX" Type="String" />
<asp:Parameter Name="DateUpdated" Type="DateTime" />
<asp:Parameter Name="ActiveMember" Type="Boolean" />
<asp:Parameter Name="UserLevelType" Type="Int32" />
<asp:Parameter Name="ContactID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="UserId" Type="Object" />
<asp:Parameter Name="UserSID" Type="Int32" />
<asp:Parameter Name="LocationID" Type="Int32" />
<asp:Parameter Name="IntranetUserName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="EmailAddress" Type="String" />
<asp:Parameter Name="ContactPhone" Type="String" />
<asp:Parameter Name="ContactPhoneExt" Type="String" />
<asp:Parameter Name="ContactFAX" Type="String" />
<asp:Parameter Name="DateUpdated" Type="DateTime" />
<asp:Parameter Name="ActiveMember" Type="Boolean" />
<asp:Parameter Name="UserLevelType" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试改用 RowDeleting 事件。
PS:我这台机器上没有安装VS,所以我无法亲自测试它......抱歉。
Try using the RowDeleting event instead.
PS: I don't have VS installed on this machine so I cant really test it myself... Sorry.