使用 sqlDataSource 从表和 ASPNET 成员资格提供程序中删除用户

发布于 2024-08-02 03:39:10 字数 6101 浏览 8 评论 0原文

我有一个 Gridview,它通过 sqlDataSource 显示联系人列表。

包括一个用于删除记录的项目模板 - 一个 LinkBut​​ton - 将 OnClientClick 设置为使用一些 JavaScript:“返回确认('您确定要删除此用户吗?');” 这会向用户弹出一个带有“是”或“取消”选项的小窗口。

从 Contact 表中删除用户时,我还需要使用 Membership.DeleteUser(UserName, true) 语句删除 AspNet Membership 表中的同一用户。

Membership 表的 Contact.IntranetUserName 列和 UserName 包含相同的数据值,这样就方便了。

不知道要使用什么事件,我开始使用 Gridview 的 OnRowCommand - 但无法弄清楚如何获取正在删除的行的“IntranetUserName”列的值(通过联系人表中的项目模板)并将其传递到 Membership.DeleteUser 语句。

任何关于此的C#想法都将受到高度赞赏。 也许我应该使用 GridView 的 OnRowDeleted 或 OnRowDeleting 来代替? 或者也许是 LinkBut​​tons 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2024-08-09 03:39:10

尝试改用 RowDeleting 事件。

void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  string name = e.Values["IntranetUserName"] as string;
}

PS:我这台机器上没有安装VS,所以我无法亲自测试它......抱歉。

Try using the RowDeleting event instead.

void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  string name = e.Values["IntranetUserName"] as string;
}

PS: I don't have VS installed on this machine so I cant really test it myself... Sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文