C# 从数据库C中删除所选数据#

C# 从数据库C中删除所选数据#,c#,asp.net,gridview,C#,Asp.net,Gridview,我可以知道如何使用link按钮从GridView删除行吗?我在谷歌找到的代码使用的是databoundGridView。我将根据使用DropDownList选择的信息绑定信息。谢谢 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string username; username = HttpContext.Current.User.Identity.Name;

我可以知道如何使用
link按钮从
GridView
删除行吗?我在谷歌找到的代码使用的是databound
GridView
。我将根据使用
DropDownList
选择的信息绑定信息。谢谢

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string username;
    username = HttpContext.Current.User.Identity.Name;
    if (DropDownList1.SelectedValue.Equals("Expired"))
    {
        SqlConnection conn4 = new SqlConnection(My connection);
        SqlDataAdapter adapter;
        string mySQL2;
        mySQL2 = 
            "SELECT Title,MessageStatus From Table_Message WHERE Username ='" 
            + username 
            + "' AND MessageStatus = 'Expired' AND Method = 'Email'";
        adapter = new SqlDataAdapter(mySQL2, conn4);
        conn4.Open();

        DataSet ds3 = new DataSet();
        adapter.Fill(ds3);
        //Execute the sql command
        GridView1.DataSource = ds3;
        GridView1.DataBind();
        conn4.Close();

    }
    else if (DropDownList1.SelectedValue.Equals("Pending"))
    {
        SqlConnection conn3 = new SqlConnection(My connection);
        SqlDataAdapter adapter1;
        string mySQL;
        mySQL = 
            "SELECT Title,MessageStatus From Table_Message WHERE Username ='" 
            + username 
            + "' AND MessageStatus = 'Pending' AND Method = 'Email'";
        adapter1 = new SqlDataAdapter(mySQL, conn3);
        conn3.Open();

        DataSet ds2 = new DataSet();
        adapter1.Fill(ds2);
        //Execute the sql command
        GridView1.DataSource = ds2;
        GridView1.DataBind();
        conn3.Close();
    }

如果您允许匿名用户访问列表:

这可能是一种选择:

  • 创建delete.aspx
  • 在查询中,请提取主键列(例如:Id,UId…)
  • 在网格视图中生成DataKeyNames=“Id”
  • 在linkbutton的onclick事件中,将用户重定向到delete.aspx?Id='Your data Id'
  • delete.aspx只能由授权用户访问。避免意外数据丢失
  • 在delete.aspx中放置一个delete按钮,它是onclick事件,用于删除具有唯一Id的记录
  • 这可能是一种安全的做事方式
  • 如果显示列表只是为了授权用户,那么可以使用ajax编写删除代码:

    • OnClientClick():编写javascript函数,将请求发送到:
      delete.aspx?Id='Id'
      ,删除那里的记录

    您可以采取以下步骤

    1) 将网格的DataKeyName映射到表的主键

    2) 将链接按钮设置为

    <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
    <asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="/_layouts/images/DELETE.GIF"
                                AlternateText="Delete" CommandName="DeleteUser" CausesValidation="false" ToolTip="Delete"/>
                            </ItemTemplate>
    
    4) 在代码隐藏中,将实现编写为

    protected void GvwUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    int userId = 0;
    if (e.CommandName.Equals("DeleteUser"))
            {
                //get the user id
                userId = Convert.ToInt32(e.CommandArgument.ToString());
    
                //GetUser will delete the user
                if (DeleteUser(userId) > 0)
                {
                 Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "alert('User Deleted.');", true);
                }
    }
    
    protected void GvwUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    int userId = 0;
    if (e.CommandName.Equals("DeleteUser"))
            {
                //get the user id
                userId = Convert.ToInt32(e.CommandArgument.ToString());
    
                //GetUser will delete the user
                if (DeleteUser(userId) > 0)
                {
                 Page.ClientScript.RegisterStartupScript(this.GetType(), "Delete", "alert('User Deleted.');", true);
                }
    }