如何将删除链接添加到我的表,该链接将从数据库中删除记录

本文关键字:删除 链接 数据库 记录 我的 添加 | 更新日期: 2023-09-27 18:34:05

我本质上是在尝试添加一些可单击的方式来从我的表中删除或编辑条目。这些条目都保存在填充表的访问数据库中。我最大的问题是我不确定如何对可点击方法进行编程,以便它保存我尝试编辑/删除的用户名。任何建议将不胜感激。

相关代码:

主.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="main" %>
<%@ Reference Control="~/UserInfoBoxControl.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder runat="server" ID="phUserInfoBox" />
    </div>
        <asp:Button id="login" runat="server"  Text="edit profile" onclick="btnRegister_click" />
        <asp:Button id="create" runat="server"  Text="logout" onclick="btnLogout_click" />
    </form>
</body>
</html>

主.aspx.cs

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class main : System.Web.UI.Page
{
    private OleDbConnection bookConn;
    private OleDbCommand oleDbCmd = null;
    private String connParam = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='F:'test'Database21.accdb'; Persist Security Info=False;";
    protected void Page_Load(object sender, EventArgs e)
    {
        {
            OleDbDataReader reader;
            bookConn = new OleDbConnection(connParam);
            bookConn.Open();
            oleDbCmd = new OleDbCommand("SELECT user_name, fname, lname  FROM profiles",bookConn);
            reader = oleDbCmd.ExecuteReader();
            while (reader.Read())
            {
                UserInfoBoxControl MyUserInfoBoxControl =(UserInfoBoxControl)LoadControl("UserInfoBoxControl.ascx");
                phUserInfoBox.Controls.Add(MyUserInfoBoxControl);
                MyUserInfoBoxControl.UserName = reader.GetString(0);
                MyUserInfoBoxControl.FirstName = reader.GetString(1);
                MyUserInfoBoxControl.LastName = reader.GetString(2);
            }
            bookConn.Close();
        }
    }
    protected void btnRegister_click(object sender, EventArgs e)
    {
        Response.Redirect("myprofile.aspx");
    }
    protected void btnLogout_click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
}

UserInfoBoxControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserInfoBoxControl.ascx.cs" Inherits="UserInfoBoxControl" %>

<table>
  <tr>
    <th>UserName</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr>
    <td><%= this.UserName %>  </td>
    <td><%= this.FirstName %></td>
    <td><%= this.LastName %></td>
  </tr>
</table>

UserInfoBoxControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{
    private string userName;
    private string fname;
    private string lname;

    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }
    public string FirstName
    {
        get { return fname; }
        set { fname = value; }
    }
    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

如何将删除链接添加到我的表,该链接将从数据库中删除记录

您需要

使用模型绑定,在其中定义要在网格中显示的类类型。

http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/updating-deleting-and-creating-data

然后,您将在网格视图中指定用于更新/删除/等记录的命令。在这些方法中,您将获得适用的信息并删除。防爆网格:

<asp:GridView>

然后注意 id 是如何传入的:

public void studentsGrid_DeleteItem(int studentID)
{
    using (SchoolContext db = new SchoolContext())
    {
        var item = new Student { StudentID = studentID };
        db.Entry(item).State = EntityState.Deleted;
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            ModelState.AddModelError("", 
              String.Format("Item with id {0} no longer exists in the database.", studentID));
        }
    }
}

如果这是您第一次使用 Web 表单,我会诚实地考虑走 MVC 路线并使用 MVC 脚手架。您不能使用 Access 数据库,但对于实体框架,您需要像上面一样直接使用 OLEDB 代码(或者如果可以选择,则不使用访问权限(

我最终弄清楚了。我所做的只是在 Web 用户控制框中添加一个按钮,并在代码隐藏中创建一个新的会话变量。

UserInfoBoxControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserInfoBoxControl.ascx.cs" Inherits="UserInfoBoxControl" %>

<table>
  <tr>
    <th>UserName</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr>
    <td><%= this.UserName %>   </td>
    <td><%= this.FirstName %></td>
    <td><%= this.LastName %>   <asp:Button id="login" runat="server"  Text="edit user" onclick="btnEdit_click" /></td>

  </tr>

</table>

UserInfoBoxControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{
    private string userName;
    private string fname;
    private string lname;

    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }
    public string FirstName
    {
        get { return fname; }
        set { fname = value; }
    }
    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnEdit_click(object sender, EventArgs e)
    {
        Session["UserNameMod"] = userName;
        Response.Redirect("userinfo.aspx");
    }
}