2011-05-25 68 views
0

我有一個複選框,它具有一些複選框的控件。我想複製中繼器中除複選框之外的所有項目,在這裏我將複製複選框的值。目的是希望將所有信息從中繼器導出爲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

回答

0

您在評論中寫道,要導出您的中繼器有一個複選框。不允許將複選框導出爲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(); 
}