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

时间:2009-08-15 05:28:43

标签: c# gridview asp.net-membership sqldatasource

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

包含用于删除记录的项目模板 - 一个LinkBut​​ton - OnClientClick设置为使用一些JavaScript:“return confirm('你确定要删除这个用户吗?');”这样,用户可以弹出一个“是”或“取消”选项。

当从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>

1 个答案:

答案 0 :(得分:1)

请尝试使用RowDeleting事件。

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

PS:我没有在这台机器上安装VS,所以我不能自己测试它...抱歉。