从数据库C#中删除选定的数据

时间:2012-09-13 06:51:34

标签: c# asp.net gridview

我是否知道如何使用GridViewLinkButton删除行?我在谷歌中找到的代码正在使用数据绑定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();
    }

2 个答案:

答案 0 :(得分:0)

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

这可能是一个选择:

  1. 创建delete.aspx
  2. 在您的查询中,请获取主键列(例如:Id,UId ...)
  3. 在网格视图中,使DataKeyNames =“Id”
  4. 在linkbutton的onclick事件中,将用户重定向到delete.aspx?Id ='您的数据ID'
  5. 只有授权用户才能访问delete.aspx。节省意外数据丢失。
  6. 在delete.aspx中输入一个删除按钮,然后它的onclick事件删除具有该唯一ID的记录。
  7. 这可能是一种安全的做事方式。
  8. 如果您只显示列表以授权用户,那么您可以使用ajax编写删除代码:

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

答案 1 :(得分:0)

您可以执行以下步骤

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

2)将LinkBut​​ton作为

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

3)在rowdatabound事件中绑定命令参数

 protected void GvwUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        ImageButton imgBtnDelete;


        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            imgBtnDelete = (ImageButton)e.Row.FindControl("imgBtnDelete");
            imgBtnDelete.CommandArgument = gvwUser.DataKeys[e.Row.RowIndex].Value.ToString();

        }
    }

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);
            }
}