简体   繁体   English

GridView不能删除值asp.net

[英]GridView cannot be delete value asp.net

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    string VisitorManagementConnectionString = ConfigurationManager.ConnectionStrings["VisitorManagementConnectionString"].ConnectionString;
    string strQuery = "select Id, ItemName, FoundAt, TimeIn, ImageName from LostFound order by ID";
    SqlCommand cmd = new SqlCommand(strQuery);
    SqlConnection con = new SqlConnection(VisitorManagementConnectionString);
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    ViewState["dt"] = dt;
    BindGrid();
    try
    {
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
        dt.Dispose(); 

    } 
}
protected void BindGrid()
{
    GridView1.DataSource = ViewState["dt"] as DataTable;
    GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void BtnLose_Click(object sender, EventArgs e)
{
    Response.Redirect("SecurityLost.aspx");
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string ID = e.Row.Cells[0].Text;
        foreach (Button button in e.Row.Cells[5].Controls.OfType<Button>())
        {
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + ID + "?')){ return false; };";
            }
        }
    }
}

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int ID = Convert.ToInt32(e.RowIndex);
    DataTable dt = ViewState["dt"] as DataTable;
    dt.Rows[ID].Delete();
    string VisitorManagementConnectionString = ConfigurationManager.ConnectionStrings["VisitorManagementConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(VisitorManagementConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("DELETE FROM LostFound WHERE ID = @ID"))
        {
            cmd.Parameters.AddWithValue("@ID", ID);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    BindGrid();
}

when i press delete button the row can be delete but when i reload the page the row i deleted still appeal back and my database also never be deleted. 当我按下删除按钮时,该行可以被删除,但是当我重新加载页面时,我删除的行仍然会受到影响,并且我的数据库也永远不会被删除。

Can someone guide me where am I going wrong? 有人可以指导我我去哪里了吗?

          <asp:GridView ID="GridView1" runat="server" OnRowDataBound = "OnRowDataBound" AutoGenerateColumns = "false" OnRowDeleting="OnRowDeleting" Font-Names = "Arial" Caption = "Lose & Found" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField = "ID" HeaderText = "ID" />
                 <asp:BoundField DataField = "ItemName" HeaderText = "Item Name" />
                <asp:BoundField DataField = "FoundAt" HeaderText = "Place" />
                <asp:BoundField DataField = "TimeIn" HeaderText = "Time Found" />
                <asp:ImageField DataImageUrlField = "ID" DataImageUrlFormatString = "Image.aspx?ImageID={0}" ControlStyle-Width = "100" ControlStyle-Height = "100" HeaderText = "Preview Image"/>
                <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
            </Columns> 
         </asp:GridView>

This my HTML file. 这是我的HTML文件。

So from the comments we've established that the problem is that your ID is always 0.To make sure the ID comes through correctly you need to make two changes: 因此,从注释中我们可以确定问题出在您的ID始终为0,要确保ID正确通过,您需要进行以下两项更改:

1.Change the Page_Load() event like this: 1.按如下所示更改Page_Load()事件:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        //Cut and paste the code to bind to the GridView here
    }
}

2.Add this code to the RowDeleting event: 2.将此代码添加到RowDeleting事件中:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int id = Int32.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM