Solved

Gridview - Excel error

Posted on 2011-02-15
4
598 Views
Last Modified: 2012-06-21
When I tried to export Gridview to Excel file, I got this error. Is this because of Itemtemplate?

Control 'GridView1_ctl14_ctl02' of type 'DataControlLinkButton' must be placed inside a form tag with runat=server.



   <asp:TemplateField HeaderText="ModifiedDate">
                                    <ItemTemplate>
                                        <asp:HyperLink ID="HyperLink1" runat="server"
                                            navigateurl='<%# string.format("test.aspx?dateordered={0}", Databinder.Eval(container.Dataitem, "dateordered", "{0:yyyy-MM-dd HH:mm:ss.fff}")) %>'
                                            text='<%# Databinder.Eval(container.dataitem, "ModifiedDate", "{0:yyyy-MM-dd HH:mm:ss.fff}") %>'></asp:HyperLink>
                                    </ItemTemplate>
                                </asp:TemplateField>  
0
Comment
Question by:VBdotnet2005
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 34900336
NOt really but it depends how your code is structured,check:
Are you following th esame steps of this link to do your export?
http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx

If more help required you must post your export excel code.
0
 

Author Comment

by:VBdotnet2005
ID: 34900935
this is my code to export to Excel

 Dim hf As New HtmlForm()
        Dim GridView2 As GridView = Me.GridView1
        hf.Controls.Add(GridView2)

        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim oStringWriter As New System.IO.StringWriter
        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
        GridView2.RenderControl(oHtmlTextWriter)
        Response.Write(oStringWriter.ToString())
        Response.[End]()
0
 

Author Comment

by:VBdotnet2005
ID: 34901952
i added gridview1.databind . that works, but why do I get this error?
ee.bmp
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 34902187
You are not rendering the gridview properly:
For starters, your posted code:
- No need an html form or Gridview2 reference.
- No attachment defined

Your code should look like:
        Dim attachment As String = "attachment; filename=YourFilename.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel"
        Dim oStringWriter As New System.IO.StringWriter
        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
        Me.GridView1.RenderControl(oHtmlTextWriter)
        Response.Write(oStringWriter.ToString())
        Response.End()

P.S. Read the instructions on the link, it is straight forwards. You may need to implement your own PrepareGridViewForExport in the button that starts export. I know the code is C# but it is easy to translate (do not forget to add vb.net zone next time - you can specify up to 3 zones)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now