Gridview Must Be Placed Inside a Form Tag with Runat="Server" Even After the Gridview Is Within a Form Tag

GridView must be placed inside a form tag with runat=server even after the GridView is within a form tag

You are calling GridView.RenderControl(htmlTextWriter), hence the page raises an exception that a Server-Control was rendered outside of a Form.

You could avoid this execption by overriding VerifyRenderingInServerForm

public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}

See here and here.

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

protected void Page_Load(object sender, EventArgs e)
{
btnExcel_Click +=................
if (!IsPostBack)
{
BindGridview();
}
}

protected void BindGridview()
{
gvdetails.DataSourceID = "dsdetails";
}

public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
BindGridview();
//Change the Header Row back to white color
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Use this code to download data from gridView to excel.

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

I think, you can use below the example.

.aspx file contens

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:Button ID="btntoExcel" runat="server" Text="GridView to Excel" onclick="btntoExcel_Click" />
</form>
</body>
</html>

.aspx.vb file contents

Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
Dim i As Integer = 0
Dim sql As String = Nothing
Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****"
sql = "select * from stores"
Dim connection As New SqlConnection(connetionString)
connection.Open()
Dim command As New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End Sub
Protected Sub btntoExcel_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.ClearContent()
Response.AddHeader("content-disposition", "attachment; filename=gvtoexcel.xls")
Response.ContentType = "application/excel"
Dim sw As New System.IO.StringWriter()
Dim htw As New HtmlTextWriter(sw)
GridView1.RenderControl(htw)
Response.Write(sw.ToString())
Response.[End]()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
'Tell the compiler that the control is rendered
'explicitly by overriding the VerifyRenderingInServerForm event.
End Sub
End Class

getting error 'must be placed inside a form tag with runat=server', but it is

Apparently the below code fixes the issue, although honestly I'm not sure why, since the original error message isn't accurate:

public override void VerifyRenderingInServerForm(Control control) 
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET server
control at run time. Used to avoid issue using RenderControl above */
}

Apparently, this overrides some built-in method that I'm not aware of, which is doing something that causes the error. Overriding it with no code prevents whatever is happening by default and thus eliminates the error.

This "fix" was mentioned in some other posts, but I didn't think it applied because the error message doesn't line up with the code.

I haven't noticed any detrimental issues from doing this.

Control 'c1_t1' of type TextBox must be placed inside a form tag with runat=server in Asp.Net

As the error states:

"Control 'c1_t1' of type 'TextBox' must be placed inside a form tag with runat=server."

And your form element is:

<form>

The error is telling you want to do. The form element needs runat="server":

<form runat="server">

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

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ad As New results()
Dim dt As results.ResultsDataTable
dt = ad.Read()

Dim attachment As String = "attachment; filename=USurvey.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dc As DataColumn In dt.Columns
Response.Write(tab + dc.ColumnName)
tab = vbTab
Next
Response.Write(vbLf)

Dim i As Integer
For Each dr As DataRow In dt.Rows
tab = ""
For i = 0 To dt.Columns.Count - 1
Response.Write(tab & dr(i).ToString())
tab = vbTab
Next
Response.Write(vbLf)
Next
Response.[End]()
'export to excel
End Sub


Related Topics



Leave a reply



Submit