Solved

Gridview - Excel error

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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