问题描述
我正在尝试在Gridview中实现一个简单的示例,其中在Gridview中有两个checkboxes(headertemplate("chkall")
和itemtemplate("chksingle"))
.我在SQL Server 2008中使用C#4.0.
在这个简单的程序中,我遇到两个问题:
a)每当我尝试检查/取消选中chkall(其autopostback属性设置为"true")时,它都不会工作.
b)选中某些复选框,然后单击Web窗体上某处的删除"按钮,它不起作用...
请帮助我纠正以下代码....
Hi, i am trying to implement a simple example of bubbling in Gridview WITH two checkboxes(headertemplate("chkall")
and itemtemplate("chksingle"))
in Gridview. I m using c# 4.0 with sql server 2008.
I have two issues in this simple program:
a) whenever i am trying to check/uncheck the chkall(whose autopostback proerty is set to ''true''), it does nt work.
b) WHen i chk on some checkboxes, and click on a ''delete'' button somewhere on the web-form, it doesnot work...
please help me rectifying the following code....
protected void chkall_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chksingle = (CheckBox)row.Cells[0].FindControl("chksingle");
chksingle.Checked = ((CheckBox)sender).Checked;
}
}
void DBind()
{
con = new SqlConnection("server=.;database=shikhar;integrated security=true");
adap = new SqlDataAdapter("select * from emp", con);
ds = new DataSet();
adap.Fill(ds, "emp");
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=shikhar;integrated security=true");
con.Open();
foreach (GridViewRow row in GridView2.Rows)
{
CheckBox chksingle = row.Cells[0].FindControl("chksingle") as CheckBox;
if (chksingle.Checked)
{
SqlCommand cmd = new SqlCommand("delete employee where id=''"+row.Cells[1].Text+"''",con);
cmd.ExecuteNonQuery();
}
}
con.Close();
con.Dispose();
DBind();
}
}
[Edited]代码被包装在"pre"标签中[/Edited]
[edit]紧急情况已删除:这可能对您来说很紧急,但对我们而言并非如此.您所强调的紧迫性只是使我们认为您离开得太晚了,并希望我们为您做这件事.这使某些人烦恼,并且可能减慢响应速度. -OriginalGriff [/edit]
Code is wrapped in "pre" tags[/Edited]
[edit]Urgency deleted: It may be urgent to you, but it isn''t to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response. - OriginalGriff[/edit]
推荐答案
这篇关于在带有两个复选框的asp.net中冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!