471,594 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

error in exporting data into excel

Hello,

I am trying to export data to excel from datagrid, and I am getting an
error: "The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)."

Error details:
System.Web.HttpException was unhandled by user code
Message="The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.ControlCollection.Add(Control child)
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 131
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 123
at datawarehouse.individual._default.excelExport(Obje ct sender,
ImageClickEventArgs e) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 173
at System.Web.UI.WebControls.ImageButton.OnClick(Imag eClickEventArgs e)
at System.Web.UI.WebControls.ImageButton.RaisePostBac kEvent(String
eventArgument)
at
System.Web.UI.WebControls.ImageButton.System.Web.U I.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
------------------------------------------------------------------------------------------------

Code that I am using for export:

Code:

private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}

if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") !=
null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal); //on this
line I am getting an error
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedIte m").GetValue(control,
null);
}
catch
{
//error msg
}

control.Parent.Controls.Remove(control);
}

else

if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetV alue(control, null);
control.Parent.Controls.Remove(control);
}
}
return;
}

public void excelExport(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//export to excel
lblTitle.Text = "All individuals";

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);

this.ClearControls(IndividualList1);
lblTitle.RenderControl(oHtmlTextWriter);
IndividualList1.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
}

Maybe have anybody ideas how to solve this problem and fix this error.

Thanks is advance.

A.

Jan 18 '07 #1
0 1192

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by bobh | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.