Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello !

 

How can I export gridView to Excel ? I'm using ASP.NET 1.1 code:

Response.ContentType = "application/vnd.ms-excel"
	Response.Charset = ""
	'Me.EnableViewState = False
	Dim tw As New System.IO.StringWriter
	Dim hw As New System.Web.UI.HtmlTextWriter(tw)
	gvw.RenderControl(hw)
	Response.Write(tw.ToString)
	Response.End()

that works with 1.1 but here (2.0) this code rise error:

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

Any idea ?

  • 10 months later...
Posted (edited)

My code for saving GridView to XLS

 

Did you ever figure this out? I am getting the same message.
This is how I am doing it:

 

Protected Sub btnSaveToXls_Click(ByVal sender As Object, _




ByVal e As System.EventArgs) Handles btnSaveToXls.Click
[indent]'WARNING: In order to do this I had to set'EnableEventValidation = "false" in the Page definition

'which can open security holes, but since I am not requesting
'any user input in this page, I don't think it will cause any
'major issues

Dim gv As GridView = gvQcTestData

If gv.Rows.Count.ToString + 1 < 65536 Then[indent]gv.AllowPaging = Falsegv.DataBind()

Dim tw As New StringWriter
Dim hw As New HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("content-disposition", _
"attachment;filename=TestData.xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
gv.AllowPaging = True
gv.DataBind()

[/indent]
End If[/indent]End Sub

Edited by PlausiblyDamp
Posted

Thatnks for the response. I found several posts on other boards that had to do the equivalent. Here's the relative snippet I came up with. Slightly different that yours (plus it's in C#) but it's essentially the same thing (dg is the GridView I'm passing into the method):

 

GridView newDG = dg;
newDG.AllowPaging = false;
newDG.AllowSorting = false;
newDG.EnableViewState = false;
newDG.CssClass = string.Empty;
ClearControls(newDG);
System.Web.UI.Page pg = new Page();
System.Web.UI.HtmlControls.HtmlForm frm = new System.Web.UI.HtmlControls.HtmlForm();
frm.Controls.Add(newDG);
pg.Controls.Add(frm);
               
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);

pg.RenderControl(htmlTW);

  • 1 year later...
Posted

Change my code to and then turned on the Sorting on the GridView

and i still get the same error.

 

Sub doExcel(ByVal Source As Object, ByVal E As EventArgs)

GridView1.DataBind()

Dim tw As New StringWriter()

Dim hw As New System.Web.UI.HtmlTextWriter(tw)

Dim frm As HtmlForm = New HtmlForm()

Response.ContentType = "application/vnd.ms-excel"

Response.AddHeader("content-disposition", "attachment;filename=CustomerSalesData.xls")

'Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")

Response.Charset = ""

EnableViewState = False

Controls.Add(frm)

GridView1.CssClass = String.Empty

frm.Controls.Add(GridView1)

frm.RenderControl(hw)

Response.Write(tw.ToString())

Response.End()

GridView1.AllowPaging = "false"

GridView1.AllowSorting = "false"

GridView1.EnableViewState = "false"

GridView1.EnableSortingAndPagingCallbacks = "false"

frm.Controls.Add(frm)

GridView1.DataBind()

End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...