Deleting records from GridView and DB using (Check-box and delete button)

ぃ、小莉子 提交于 2019-12-13 04:05:12

问题


I have a GridView1 declared as:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" DataKeyNames="Case_ID">
  <Columns>
      <asp:TemplateField>
           <ItemTemplate><asp:CheckBox ID="cb" runat="server" /></ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField  HeaderText="No.">
           <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField  HeaderText="Case Title"  DataField="caseTitle"/>
      <asp:BoundField  HeaderText="Age" DataField="age" />
      <asp:BoundField  HeaderText="Gender"  DataField="gender"/>
      <asp:BoundField  HeaderText="Treated By" DataField="owner"/>
      <asp:BoundField  HeaderText="Added Date"  DataField="sDate"/>
      <asp:BoundField  HeaderText=""  DataField="Case_ID" Visible="false"/>
  </Columns>
</asp:GridView>

and this is the DataTable I'm using as a DataSource for it

DataTable aTable = new DataTable();

aTable.Columns.Add("caseTitle", typeof(string));
aTable.Columns.Add("age", typeof(string));
aTable.Columns.Add("gender", typeof(string));
aTable.Columns.Add("owner", typeof(string));
aTable.Columns.Add("sDate", typeof(string));
aTable.Columns.Add("Case_ID", typeof(int));
I want to use Case_ID as an index for the GridView, so I can delete a record from database when I check its check box in the GridView.

here is the scenario:

GridView displays cases information from database User can check check-boxes and then click [Delete] button, this action should delete the records from database;

When I execute this code in [Delete] button:

foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox c = (CheckBox)row.FindControl("cb");

    if (c.Checked)
    {
        int rowIndex = GridView1.SelectedIndex;
        string id = GridView1.DataKeys[rowIndex].Value.ToString();
        ds.DeleteCommand = " delete from Cases where Case_ID=" + id + "";
        ds.Delete();
    } 
}

I found that c is always null even when I'm checking some records; so the problem is its not detecting the change in the checkbox what can I do to make it detected?


回答1:


Where's the delete button... try finding the fitting checkbox button by using the "argument" attribute of the delete button. I usually put the ID of the row that I wish to delete as the argument attribute value of the button. That's why I never need to search for the checkbox... (on client side I use the client click event of the checkbox to update the argument attribute of the delete button).

Another word on security: use stored procedure to avoid security issues (never put delete statement - or any SQL statement - as hard coded in C#...).

EDIT:

check also:

     GridViewRow.RowType==RowType.DataRow 

You get null because the current row is possibly an header row, or something like that - and the checkbox is not a part of this kind of row.



来源:https://stackoverflow.com/questions/9353429/deleting-records-from-gridview-and-db-using-check-box-and-delete-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!