0

我使用由 SQL Server Management Studio 备份的 Visual Studio 2012 作为其数据库。我正在使用 gridview 来查看我的数据。我使用了一个可以编辑和删除数据的 TemplateField。但似乎当我删除某个数据时,它总是删除第一个数据。解决方案可能是什么?

这是我的asp代码:

<asp:Content ID="Content4" ContentPlaceHolderID="full_main" Runat="Server">
    <asp:GridView ID="gvUsers" runat="server" 
    class="table table-bordered data-table" AutoGenerateColumns="False" 
    AllowPaging="True" onpageindexchanging="gvUsers_PageIndexChanging" 
        onselectedindexchanged="gvUsers_SelectedIndexChanged" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="UserID" HeaderText="User ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="UserID" />
            <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                SortExpression="LastName" />
            <asp:BoundField DataField="EmailAddress" HeaderText="Email Adress" SortExpression="EmailAddress" />
            <asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" />
            <asp:BoundField DataField="Municipality" HeaderText="Municipality" SortExpression="Municipality" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" SortExpression="ZipCode" />
            <asp:BoundField DataField="ContactNo" HeaderText="Contact No" SortExpression="ContactNo" />
            <asp:TemplateField HeaderText="Actions">
                <ItemTemplate>
                    <a href="http://localhost:12345/CAPSTONE/Users/Edit.aspx?ID=<%# Eval("UserID") %>" 
                                class="btn btn-primary btn-mini">Edit</a>
                            <a href="#deleteItem" data-toggle="modal" class="btn btn-danger btn-mini">Delete</a>
                            <div id="deleteItem" class="modal hide">
                              <div class="modal-header">
                                <button data-dismiss="modal" class="close" type="button">×</button>
                                <h3>Record Deletion</h3>
                              </div>
                              <div class="modal-body">
                                <p>Are you sure you want to delete the record?</p>
                              </div>
                              <div class="modal-footer"> 
                                <a class="btn btn-primary"
                                    href="http://localhost:12345/CAPSTONE/Users/Delete.aspx?ID=<%# Eval("UserID") %>">Confirm</a> 
                                <a data-dismiss="modal" class="btn" href="#">Cancel</a> 
                              </div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            <center>
                <h1>
                    No records found.</h1>
            </center>
        </EmptyDataTemplate>
        <PagerStyle CssClass="pagination alternate" />
    </asp:GridView>

这是我背后的代码:

void DeleteUser(int userID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "DELETE FROM Users WHERE UserID=@UserID";
        cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = userID;
        cmd.ExecuteNonQuery();
        con.Close();
        Helper.AddLog(Session["userid"].ToString(), "Delete", "Deleted a User");
        Response.Redirect("Default.aspx");
    }
4

0 回答 0