.net2.0中如何得到gridview中动态添加的模板列中checkbox控件是否选择
public class MutiSelectTemplate : ITemplate
{
public MutiSelectTemplate(DataControlRowType type)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private DataControlRowType templateType;
#region ITemplate 成员
public void InstantiateIn(Control container)
{
if (templateType == DataControlRowType.DataRow)
{
CheckBox checkbox = new CheckBox();
checkbox.ID = "SelectMutiWTD ";
container.Controls.Add(checkbox);
}
}
#endregion
}
protected void Page_Load(object sender, EventArgs e)
{
BindGridViewCell(GridView1);
}
private void BindGridViewCell(GridView gridView)
{
......
TemplateField templateField = new TemplateField();
templateField.HeaderText = "选择 ";
templateField.ItemTemplate = new MutiSelectTemplate();
gridView.Columns.Add(templateField);
}
//点击保存按钮的时候从gridview中得到所有行,判断是否选择
protected void SaveButtonClick(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if ((GridView1.Rows[i].Cells[4].FindControl( "SelectMutiWTD ") as CheckBox).Checked)//运行时候错误,为null,找不到控件checkbox
{
.....
}
}
}
在网上也找了,但没有找到解决办法,请求各位大哥帮忙
[解决办法]
<asp:TemplateField HeaderText= "<input type= 'checkbox ' id= 'chk ' name= 'chk ' onclick= 'selectAndCancelall(this.checked); ' />选中全部 " FooterText= "全选 ">
<ItemTemplate>
<input type= "checkbox " id= "checkboxname " name= "checkboxname " value= ' <%# DataBinder.Eval(Container.DataItem, "USER_ID ")%> ' onclick= 'SingleCheckJs(); ' runat= "server " />
</ItemTemplate>
<ItemStyle Wrap= "False " />
<HeaderStyle Wrap= "False " />
</asp:TemplateField>
JS:
function SingleCheckJs()
{
var flag1=false;
var flag2=false;
if (document.aspnetForm.checkboxname.length)
{
for (var i=0;i <document.aspnetForm.checkboxname.length;i++)
{
if(document.aspnetForm.checkboxname[i].checked)
flag1 = true;
else
flag2 = true;
}
}
else
{
if(document.aspnetForm.checkboxname.checked)
flag1 = true;
else
flag2 = true;
}
if(flag1==true&&flag2==false)
document.getElementById( "chk ").checked = true;
else
document.getElementById( "chk ").checked = false;
}