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)
{
return;
}

found it here: http://blogs.x2line.com/al/archive/2004/10/08/576.aspx

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!

10 comments:

Ones_and_Zeros said...

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

Jay said...

This worked like a charm. Thanks for sharing this.

ajitatif angajetör said...

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

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

Unknown 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