从GridView的删除记录,并使用DB(复选框和删除按钮) [英] Deleting records from GridView and DB using (Check-box and delete button)
问题描述
我有一个GridView1声明为:
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>
这是我使用作为它一个DataSource
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.
这里的情景:
GridView控件显示来自数据库信息的情况下
用户可以检查在方框中打勾,然后单击[删除]按钮,这个动作应该删除数据库中的记录;
GridView displays cases information from database User can check check-boxes and then click [Delete] button, this action should delete the records from database;
当我在执行此code [删除]按钮:
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();
}
}
我发现,c是永远甚至当我检查的一些记录为空;所以这个问题是它不是检测的复选框,我能做些什么来让它检测到的变化呢?
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?
推荐答案
在哪里删除按钮...尝试通过使用参数的删除按钮的属性,找到合适的复选框按钮。我通常把我希望删除按钮的参数属性值的行的ID。这就是为什么我从来没有需要搜索的复选框。(在客户端我用的复选框的客户端点击事件来更新删除按钮的参数属性)。
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).
在安全的另一个词:使用存储过程,以避免安全问题(从来没有把delete语句 - 或任何SQL语句 - 在C#硬codeD ...)
Another word on security: use stored procedure to avoid security issues (never put delete statement - or any SQL statement - as hard coded in C#...).
编辑:
检查也:
GridViewRow.RowType==RowType.DataRow
您获得null,因为当前行可能是一个标题行,或类似的东西 - 而该复选框是不是这种行的一部分
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.
这篇关于从GridView的删除记录,并使用DB(复选框和删除按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!