GridView to Excel error

Discussion in 'ASP .Net' started by David, Oct 20, 2006.

  1. David

    David Guest

    Using ASP.Net 2.0 and I have identical code for exporting a GridView to
    Excel in 2 web sites. One works fine and the 2nd one gives me the error
    "RegisterForEventValidation can only be called during Render();"

    This error occurs on the line that reads "gv.RenderControl(hw)"

    Below is my code. Can anybody give me a clue? Thanks.

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

    Dim gv As GridView = SearchResultsGridView

    If gv.Rows.Count.ToString + 1 < 65536 Then


    gv.AllowPaging = False

    gv.AllowSorting = False

    If txtSearchText.Text <> "" Then

    SqlvwFileSearch.SelectCommand = txtSearchText.Text

    End If

    gv.DataBind()


    Dim tw As New StringWriter()

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

    Response.Clear()

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

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

    Response.Charset = ""

    EnableViewState = False

    gv.RenderControl(hw)

    Response.Write(tw.ToString())

    Response.End()

    'gv.AllowPaging = True

    gv.AllowSorting = True

    gv.DataBind()

    lblErrorMsg.Text = ""

    Else

    lblErrorMsg.Text = "Too many rows - Export to Excel not possible"

    End If

    End Sub


    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As
    System.Web.UI.Control)

    End Sub

    David
     
    David, Oct 20, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,983
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  2. =?Utf-8?B?bWFuaWthMDI=?=

    Export GridView to Excel Error

    =?Utf-8?B?bWFuaWthMDI=?=, Feb 21, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    1,066
    Larry Bud
    Feb 26, 2007
  3. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,526
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  4. Replies:
    0
    Views:
    428
  5. JohnE
    Replies:
    6
    Views:
    5,200
    praveenbattula
    Oct 9, 2010
Loading...

Share This Page