在GridView中,最前列我放了一个CheckBox,现在我有一个CheckBox的框设置enable的属性为false,也就是不可用了,但是我点击全选Button的时候,这个框子还是会被选中,这个如何解决?
我按钮的事件是这样的:
protected void btnSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.gvHD.Rows.Count; i++)
{
CheckBox chk = (CheckBox)this.gvHD.Rows[i].FindControl("MsgID");
chk.Checked = true;
}
}
我按钮的事件是这样的:
protected void btnSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.gvHD.Rows.Count; i++)
{
CheckBox chk = (CheckBox)this.gvHD.Rows[i].FindControl("MsgID");
chk.Checked = true;
}
}
protected void btnSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.gvHD.Rows.Count; i++)
{
CheckBox chk = (CheckBox)this.gvHD.Rows[i].FindControl("MsgID");
if(chk.Enabled)
{
chk.Checked = true;
}
}
}
{
for (int i = 0; i < this.gvHD.Rows.Count; i++)
{
CheckBox chk = (CheckBox)this.gvHD.Rows[i].FindControl("MsgID");
if (chk.Enabled=false)
{
chk.Checked = false;
}
chk.Checked = true;
}
}
判断我也试过了,没用不仅没用,写了这个判断后,当我点击全选后,依然可以全部选中,并且所有的Checkbox都变成灰色不可用的了
if(chk.Enabled)
{
chk.Checked = true;
}
而checkbox 灰化 当然他还是checkbox当然是会被选中的!
{
continue;
}
这个效果跟你要的是一样的! 仅供你学习参考!页面前台很简单,就是一个数据源控件,绑定数据,就给你描述的那样!
这个是核心的代码, 另外在给你附加一个 选中获取各个值的代码!!!!
代码:
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
if (CheckBox2.Checked == true)
{
for (int i = 0; i < Repeater1.Items.Count; i++)
{
((CheckBox)Repeater1.Items[i].FindControl("cb1")).Enabled = false;
((CheckBox)Repeater1.Items[i].FindControl("cb1")).Checked = true;
}
}
else if (CheckBox2.Checked == false)
{
for (int i = 0; i < Repeater1.Items.Count; i++)
{
((CheckBox)Repeater1.Items[i].FindControl("cb1")).Enabled = true;
((CheckBox)Repeater1.Items[i].FindControl("cb1")).Checked = false;
}
}
} 页面有个按钮,获取选中的值! 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (CheckBox2.Checked == false)
{
for (int i = 0; i < Repeater1.Items.Count; i++)
{
if (((CheckBox)Repeater1.Items[i].FindControl("cb1")).Checked == true)
{ Response.Write(((Button)Repeater1.Items[i].FindControl("Button2")).CommandArgument.ToString()); }
} }
else
{
for (int i = 0; i < Repeater1.Items.Count; i++)
{ Response.Write( ((Button)Repeater1.Items[i].FindControl("Button2")).CommandArgument.ToString()); }
}
}
{
CheckBox chk = (CheckBox)this.GridView1.Rows[i].FindControl("MsgID");
if (chk.Enabled == true)
{
chk.Checked = true;
}
}