Я использую чекбоксы внутри gridview .., когда я нажимаю на чекбокс, мой код не получает значение "IsChecked" .. вот код ..
protected void ButtonDelete_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
if (cb != null)
{
if (cb.Checked)...//(showing ischecked=false)///
{
id = GridView1.Rows[i].Cells[1].Text; // get the id of the field to be deleted
sc.Add(id); // add the id to be deleted in the StringCollection
}
}
}
Я удаляю данные из базы данных как..
private void DeleteRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = (ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "DELETE FROM User WHERE user_id";
sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
мой код asp ..
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="ButtonDelete" runat="server" Text="Delete" OnClick="ButtonDelete_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="user_id" HeaderText="user_id" ReadOnly="True" />
<asp:BoundField DataField="fname" HeaderText="fname"/>
<asp:BoundField DataField="lname" HeaderText="lname"/>
</Columns>
</asp:GridView>