code block error on control creation

M

Mortar

i am dynamically creating controls when i do a server side export to
an excel file. I can only get it to work if I have no <% %> anywhere
in my html.

i.e. i am writing out session name to the html <%=Session["fname"]%>,
and when i try and do the export i get:

controls collection cannot be modified because the control contains
code blocks (i.e. <% ... %>)

how else can i write session variables or other asp variables into the
html page differently?

below is the excel export...maybe something can be changed there?

private void Button2_Click(object sender, System.EventArgs e)
{
//export to excel

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);

// dgResults.GridLines = GridLines.None;
// dgResults.HeaderStyle.Font.Bold = true;
// dgResults.PagerStyle.BackColor =
System.Drawing.Color.White;
this.ClearControls(dgResults);
dgResults.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
}

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

if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch

{

}

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").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top