Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.


I wrote an article about Exporting DataGrid to Excel which can be viewed at www.GridViewGuy.com. One question that is asked again and again was how to export the Datagrid which has the link buttons in it. If you try to export a Datagrid with link buttons inside it than it will throw an exception saying "LinkButton server control must be inside the Form tags with runat = server". If you view the html source then you will see that the LinkButton is inside the form tags with runat = server.

This error can be avoided if you hide the LinkButton columns or disable them. I was just wondering that if this is some BUG in Visual Studio or something else.

Posted on Sunday, August 14, 2005 1:09 PM | Back to top


Comments on this post: Datagrid Export to Excel problems!!

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
In my opinion, because the there may be controls like checkboxes, buttons, etc., inside the datagrid, it doesn't get rendered like it "supposed" to. ASP.NET "thinks" these controls are static data since you're pulling the data from the datagrid directly or because ASP.NET can't render controls within another control. I've noticed, including this issue, that in many cases, ASP.NET can only work in one "layer" of controls or code considering master pages which ASP.NET uses a different approach.
Left by Ray Linder on Aug 16, 2005 9:33 AM

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
I've found that adding a line of code to my 'export button' that suppresses the checkboxes, buttons, etc allows the excel file to populate properly. Of course you will need to modify the column number that needs to be suppressed.

The following is my export button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

DataGrid1.Columns.Remove(DataGrid1.Columns.Item(0))

Response.Clear()
Response.Charset = ""
Response.AddHeader("content-isposition", "attachment;filename=RMA_Log.xls")
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim dg As New DataGrid
dg = DataGrid1
dg.GridLines = GridLines.Both
dg.HeaderStyle.Font.Bold = True
dg.DataBind()
dg.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()

End Sub
Left by Anthony on Sep 07, 2005 12:17 PM

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
Hi ,
I am not able to view the excel grid lines for the rest of the page while exporting data to Excel sheet from asp.net applications.
Left by Gopala Reddy on Jul 20, 2006 3:00 AM

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
Hi,

I just want to know when the Datagrid has paging, sorting enabled and I want to export the data to Excel.
How to implement it.

Can you help me in sorting it out.
Left by Senthil on Sep 07, 2006 4:13 AM

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
wen i click export to excel buttton foll erre appers:
Control '_ctl7_DataGrid1__ctl34__ctl1' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server
Left by shail on Nov 10, 2006 12:14 AM

# re: Datagrid Export to Excel problems!!
Requesting Gravatar...
That happens because it is inside a Master Page. The <form> tag is in the master page.
Left by George on Nov 23, 2006 1:03 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net