【发布时间】:2011-03-22 09:56:47
【问题描述】:
我在 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>
【问题讨论】:
-
这是 WPF 吗?然后请同时包含您的 WPF 代码。
-
让你在 page_load 中写 if(!ispostback)
-
你能把代码放在你为gridivew和page_Load代码绑定数据的地方
标签: c# asp.net gridview checkbox