简体   繁体   中英

how can i create download button in asp.net?

this is the Html source code. code is in runat server but also comming that error.

 <form id="form1" runat="server"> <asp:Panel ID="Panel1" runat="server" Visible="False" Height="302px"> <center> <table> <tr> <td> PERSONAL INFORMATION </td> </tr> <tr> <td>First Name: <asp:Label ID="fname" runat="server"></asp:Label> </br> Last Name: <asp:Label ID="lname" runat="server" ></asp:Label> </br> Age: <asp:Label ID="age" runat="server" ></asp:Label> </br> Date od Birth: <asp:Label ID="dob" runat="server" ></asp:Label> </br> Address: <asp:Label ID="add" runat="server" ></asp:Label> </br> Birth Place: <asp:Label ID="bp" runat="server" ></asp:Label> </br> Mobile No.: <asp:Label ID="mn" runat="server" ></asp:Label> </td> </tr> </table> <asp:Button ID="download" runat="server" OnClick="download_Click" Text="DOWNLOAD" /> </center> </asp:Panel> </form>

I create a download button and i run this code of that button but i fund this error "Control 'download' of type 'Button' must be placed inside a form tag with runat=server.".this my class library with code.i create a database to add detailed in webpage. and last i covert it in to pdf.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.IO;

    protected void download_Click(object sender, EventArgs e)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=DataSet.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                Panel1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10, 10, 0, 0);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
            }

You have a typo, <form> needs to be outside of ASP:Panel and like the error message states you need to set the form tag with runat=server:

<form runat="server">
    <asp:Panel ID="Panel1" runat="server" Visible="False" Height="302px">
        

    </asp:Panel>
</form>

done. button should be upper of tag.

 </center> </asp:Panel> <center> <asp:Button ID="download" runat="server" OnClick="download_Click" Text="DOWNLOAD" /> <center> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM