如何从数据库中检索arecord并在选择下拉框时在iframe中显示为pdf [英] how to retrieve arecord from database and show as pdf in iframe when dropdown box is selected

查看:57
本文介绍了如何从数据库中检索arecord并在选择下拉框时在iframe中显示为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我已编写代码,但该记录正在下载到我的系统

所以下面是我的代码任何正文帮助我哪里是错误



actually i have written code but that record is downloading in to my system
so below is my code any body help me where is the error

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

public partial class dropdown_to_gridview : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    public void GridViewBind()
    {
        string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
        SqlConnection con = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + DropDownList1.SelectedValue + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {
        string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
        SqlConnection con = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + DropDownList1.SelectedValue + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=report.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        pdfFrame.Attributes.Add("sr", "pdfDoc");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
   
}

推荐答案

将文件放入iframe你需要在客户端上看到iframe的src属性。当有人更改下拉列表时,通过javascript将iframe的src更新为/showpdf.aspx?id = ???哪里???是所选项目的值。现在创建一个名为showpdf.aspx的页面,并在其page_load事件中放入当前只在_click事件中的代码,而不是从下拉列表中获取值,从查询字符串中获取它



To put the file in the iframe you'll need to see the iframe's src property on the client. When someone changes the drop down update the src of the iframe via javascript to something like "/showpdf.aspx?id=???" where ??? is the value of the selected item. Now create a page called showpdf.aspx and in its page_load event put the code that is currently in your _click event only rather than getting a value from the drop down, get it from the querystring

SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + Request.QueryString["id"] + "'", con);





就你的查询而言,你不应该使用select *,总是命名你的列,并且不要那样做SQL,因为它是开放的sql注入攻击,改为使用参数化查询。



要回答你的下一个问题,我不会为你编写所有代码。如果你不知道如何在下拉列表更改时运行javascript,那么googlejavascript select change event。要阅读所选值谷歌javascript获取更改选择值。要更新iframe的src googlejavascript update iframe src。然后你只需把所有东西放在一起。



In terms of your query you shouldn't use "select *", always name your columns, and don't make SQL that way as it is open to sql injection attacks, used parameterised queries instead.

To answer your next question, not I won't write all the code for you. If you don't know how to run javascript when the dropdown changes then google "javascript select change event". To read the selected value google "javascript get select value on change". To update the iframe's src google "javascript update iframe src". Then you just have to put everything together.


这篇关于如何从数据库中检索arecord并在选择下拉框时在iframe中显示为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆