繁体   English   中英

从数据库中删除记录在 GridView 中不起作用

[英]Deleting record from database is not working in GridView

我正在尝试通过选择复选框然后单击删除来从我的数据库中删除一条记录。 现在似乎发生的是页面刷新但条目没有被删除。 从来没有错误消息

我已经按照本教程在 ASP.Net 中使用 CheckBox 从 Gridview 中删除记录

这就是我的代码的样子:

添加IP地址.aspx

<script type="text/javascript">  

    function DeleteConfirm() {
        var Ans = confirm("Do you want to Delete Selected Employee Record?");
        if (Ans) {
            return true;
        }
        else {
            return false;
        }
    }
</script>  

 <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
            RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
            runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkDel" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="EmpId" HeaderText="Device Id" />
                <asp:BoundField DataField="Device" HeaderText="Device Name" />
                <asp:BoundField DataField="IP" HeaderText="IP Address" />
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div> 
        <br />
        <asp:Button ID="btnDeleteRecord" runat="server" onclick="btnDeleteRecord_Click" Text="Delete" CssClass="btn-success" />

然后在:

AddIPAddress.cs.aspx

string cs = ConfigurationManager.ConnectionStrings["IPAddress"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
    {
        this.BindGrid();
        btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
    }

private void BindGrid()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter adapt = new SqlDataAdapter("SELECT * FROM IPAddress", con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }

    protected void DeleteRecord(int empid)
    {
        SqlConnection con = new SqlConnection(cs);
        SqlCommand com = new SqlCommand("DELETE FROM IPAddress WHERE EmpId=@ID", con);
        com.Parameters.AddWithValue("@ID", empid);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
    }

    protected void btnDeleteRecord_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow grow in GridView1.Rows)
        {
            //Searching CheckBox("chkDel") in an individual row of Grid  
            CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
            //If CheckBox is checked than delete the record with particular empid  
            if (chkdel.Checked)
            {
                int empid = Convert.ToInt32(grow.Cells[1].Text);
                DeleteRecord(empid);
            }
        }
        //Displaying the Data in GridView  
        BindGrid();
    }

我错过了什么吗?

谢谢

您需要执行以下步骤:-

我向您解释数据库中的所有进程删除记录在 GridView 中不起作用

 protected void btnDeleteRecord_Click(object sender, EventArgs e)  
   {    
    foreach (GridViewRow gvrow in GridView1.Rows)  
    {    
         var Label = gvrow.FindControl("Label1") as Label;  

           SqlCommand cmd = new SqlCommand("delete from tblname where id=@id",con);  
           cmd.Parameters.AddWithValue("id", int.Parse(Label.Text));  
            con.Open();  
            int id = cmd.ExecuteNonQuery();  
            con.Close();  
            refreshdata();  
        }  
    }

    public void refreshdata()  
    {   
      SqlCommand cmd = new SqlCommand("select * from tbl_data", con);  
      SqlDataAdapter sda = new SqlDataAdapter(cmd);  
      DataTable dt = new DataTable();  
      sda.Fill(dt);  
      GridView1.DataSource = dt;  
      GridView1.DataBind();
    }

好吧,我肯定在这里错过了一些东西:-)

此处的代码应更改为:

protected void Page_Load(object sender, EventArgs e)
{
    this.BindGrid();
    btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
}

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Displaying the Data  
            BindGrid();
            //Adding an Attribute to Server Control(i.e. btnDeleteRecord)  
            btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
        }
    }

现在一切正常。 感谢所有提供帮助的人:-)

暂无
暂无

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

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