复制并忽略复选框上的复选框

问题描述:

我有一个复选框,它具有一些复选框的控件。我想复制中继器中除复选框之外的所有项目,在这里我将复制复选框的值。目的是希望将所有信息从中继器导出为xls格式的Excel文档。但是,这不允许我有复选框,因此我想删除它们。复制并忽略复选框上的复选框

我该怎么做?

我曾尝试:

for (int j =0; j<repeater1.Items.Count; j++) 
{ 
    RepeaterItem repItem = repeater1.Items[j]; 

    foreach (Control c in repItem.Controls) 
    { 
     if (!(c is CheckBox)) 
     { 
      Control c2 = c; 
      repeater2.Items[j].Controls.Add(c2); 
     } 
    } 
} 

但它给我这个错误:

Collection was modified; enumeration operation may not execute.

+0

如果你的项目包含复选框,你不想展示? – 2011-05-25 08:46:27

+0

我不希望它成为中继器的一部分 - 因为那时我需要导出为ex​​cel,并且它不接受复选框 – Lilz 2011-05-25 08:57:59

+0

只需修改您的问题并解释您实际需要的内容。 – 2011-05-25 09:03:39

您在评论中写道,要导出您的中继器有一个复选框。不允许将复选框导出为ex​​cel,这就是为什么您要在导出前删除复选框的原因。

这里是你可以做到这一点的一种方式......只是在你的页面中添加这一点,并试图....

public override void VerifyRenderingInServerForm(Control control) 
{ 
} 

或者如果你导出你的DataTable代替repeater

如果以上不工作,你想迭代,然后导出然后在这里是代码...

protected void btnExport_Click(object sender, EventArgs e) 
{ 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 

    string attachment = "attachment; filename=FileName" + DateTime.Now.ToString() + ".xls"; 

    Response.ClearContent(); 
    Response.AddHeader("content-disposition", attachment); 
    Response.ContentType = "application/ms-excel"; 

    foreach (RepeaterItem item in Repeater1.Items) 
    { 
     CheckBox chk= item.FindControl("CheckBox") as CheckBox; 
     chk.Visible = false; 
    } 

    Repeater1.RenderControl(htw); 

    Response.Write(sw.ToString()); 
    Response.End(); 
}