Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gridview - Excel error

Posted on 2011-02-15
4
Medium Priority
?
624 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 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

972 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