• Register
0 votes

Problem :

I am facing below error if I try to export datagrid to the excel.
“Control 'gridview1' of type 'gridview' must be placed inside a form tag with runat=server.”

Please find below my code :

<form runat="server" id="form1">
    <div runat="server" id="div1">
        grid view:
        <asp:GridView runat="server" ID="grid1">

    <asp:TextBox runat="server" ID="text1" TextMode="MultiLine" Rows="30" Columns="60"></asp:TextBox>

My GridView is within the from tag with the runat="server", but still I am facing above error.

How to fix this?

8 4 2
2,300 points

1 Answer

0 votes

Solution :

I have came across your question on this forum. I had also encountered with the same problems for the number of times in the past and I found that it occurs when you have explicitly EnablePaging or EnableSorting set to true value.
The fix for this is to disable the sorting and the paging before your export and also make sure to re enable them after your export is done.

The above error can also occur when you want to export your GridView control to the Word, Excel, or to the any other formats. Your compiler will assume that your control is not added to your form and is rendered without it and so it will throw above error even if the GridView control is inside the form with the runat = “server”.

Now you must convey to your compiler that your control is rendered explicitly by overriding the VerifyRenderingInServerForm event.
As shown below:

public override void VerifyRenderingInServerForm(Control control)


5 2 1
4,980 points

Related questions

2 votes
1 answer 41 views
Problem : I am very new to GridView.I have already written one row Editing function to catch every edits but still my page always shows me below error. &ldquo;The gridview 'gridview1' fired event rowediting which wasn' ... (NameValueCollection postData) +9643314 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 How to fix this?
asked Mar 24 morrisBson 3.2k points
0 votes
1 answer 155 views
Problem : I am getting into the C# coding but I am having following issue: I have below code on the UserControl: using MyFirstDataLayer.Section1; public class MyFirstClass { protected void MyFirstMethod { MyFirstClass.MyFirstItem oItem = new MyFirstClass.MyFirstItem(); oItem = ... name instead I am not sure of what this actually means, I did some research but was unable to figure it out.
asked Dec 18, 2019 alecxe 7.5k points
1 vote
1 answer 39 views
Problem : I want to create the simple user control that should be the slider. When I try to add the AjaxToolkit SliderExtender to my user control I usually get below error: Server Error in '/' Application. The Controls collection cannot be modified because ... control and also tried adding the textbox and the slider extender to my placeholder programmatically but I am still facing the above error.
asked Dec 14, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
How can you find the mime type of a file based on the file signature, not the extension in .Net? I want a simple way to get a mime type where the file extension is incorrect or not given, something similar?
asked Aug 29 Aliza313 720 points
0 votes
1 answer 48 views
Problem : I came across a problem as follows if you set the autopostback property of a control to true, the page is posted back to the server Any solution on this ?
asked Nov 12, 2019 peterlaw 6.9k points