Thursday, October 25, 2007

GridView must be placed inside a form tag with runat=server

I was creating an online test and I wanted to email the test results by topic to an email distribution list. I have a panel which has two child controls, a label with an overall % correct and a gridview with percentages by topic.

A few simple lines of code would typically handle this:

string listRecipients = ConfigurationManager.AppSettings["candidateTestEmailList"].ToString();
StringBuilder sb = new StringBuilder(2000);
pnlResults.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(sb)));

// Send to the mail handler class
MailHandler mh = new MailHandler();
mh.SendMail(listRecipients, "Test Results " + tbName.Text, sb.ToString());

But I was getting the error...Gridview must be placed inside a form tag with runat=server. And of course it is, and the panel and the label are not throwing a similar error. I found this nifty fix:

public override void VerifyRenderingInServerForm(Control control)

found it here:

and it worked like a charm. I didn't spend more time investigating which controls require the fix, but if I see the error, I know where to start. Thanks Anatoly!


zigzag said...

Thanks for posting that workaround. I have been chasing my tail with that error all morning!

Jaya sekhar said...

This worked like a charm. Thanks for sharing this.

Gökalp said...

marvelous, thanks a lot!! you may also use something like this:

public override void VerifyRenderingInServerForm(Control control)
if (control != this.gridViewControl) {

José Borges said...

Thank you. Very helpful.

Facebook Pretender said...

cool man
thanks a lot

Ashok said...

Cool , saved lot of time .

khayer said...

Thanks.. It's help me lots.

Diana said...
This comment has been removed by the author.
Diana said...

Thank you veryyyy much! it's realy worked like a charm!!!!

Lakshmikanth said...

Brilliant dude,thanks a lot