Link to home
Start Free TrialLog in
Avatar of RobertNZana
RobertNZanaFlag for United States of America

asked on

Control must be placed inside a form tag

Getting this error...

Control 'mygridview' of type 'GridView' must be placed inside a form tag with runat=server.

I dynamically take a gridview, stored in a session var, and put it in the form's controls collection. Why am I getting this error?


Dim stw As New System.IO.StringWriter()
Dim htextw As New HtmlTextWriter(stw)            
Dim mygridview As New GridView

mygridview = CType(Session("GridViewToExport"), GridView)
form1.Controls.Add(mygridview)

'render gridview control to excel
 mygridview.RenderControl(htextw) ' ERROR OCCURS HERE

Open in new window

Avatar of guru_sami
guru_sami
Flag of United States of America image

Use PlaceHolder control to do that.
Add a PlaceHolder Control on the page where you want the GV to appear.
Then add gv to this ph.
e.g.

<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

PlaceHolder1.Controls.Add(mygridview)

e.g.:http://blog.krisvandermast.com/AddingADynamicControlToAPlaceholderControlAndWireUpTheEvent.aspx
Avatar of RobertNZana

ASKER

Same exact error, even when using the placeholder solution.

It happens on the rendercontrol line.
ASKER CERTIFIED SOLUTION
Avatar of timexist
timexist
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Seems to work well.

But when Excel opens the file it says "The file you are trying to open, xxxx.xls, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you wat to open the file now?"

Do you know what this means?