Solved

Gridview - Excel error

Posted on 2011-02-15
4
595 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 69
Whats wrong with this query 21 41
Hovering effect 9 30
Different Delete Messages 7 23
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

11 Experts available now in Live!

Get 1:1 Help Now