Solved

Gridview - Excel error

Posted on 2011-02-15
4
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

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 36

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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