如何在c#中保存面板的内容

本文关键字:保存 | 更新日期: 2023-09-27 17:54:55

我正在做一个c#项目,我有一个保存面板的问题。我有一个列表框,我选择了我想要加载到面板的表单。但是当我想保存我的面板时,却无法读取它的内容。保存面板后,我想将其转换为pdf文档,但当我无法保存它时,这是不可能做到的。有人能帮帮我吗?

    protected void btnPDF_Click(object sender, EventArgs e)
    {
        int teller = 0;
        foreach (Control Ctrl in pnlMain.Controls)
        {
            if (Ctrl is Label)
            {
                teller++;
            }
        }
        int teller2 = 0;
        Label[] arr_label = new Label[teller];
        foreach (Control Ctrl in pnlMain.Controls)
        {
            if (Ctrl is Label)
            {
                Label lbl_Ctrl = Ctrl as Label;
                arr_label[teller2] = lbl_Ctrl;
                teller2++;
            }
        }
        int teller3 = 0;
        int lengte = arr_label.Length;
        String hulp1;

        //aanmaken van uw document
        var doc1 = new Document(PageSize.A4, 50, 50, 25, 25);
        //aanmaken van  de outputstream
        var output = new MemoryStream(); //geen extra argumenten nodig
        //aanmaken van een "pdfwriter document"
        var writer = PdfWriter.GetInstance(doc1, output);
        //voor je in je document iets kan "schrijven" moet je het eerst openen:
        doc1.Open();
        while (teller3 < lengte)
        {
            Label hulp;
            hulp = arr_label[teller3];
            hulp1 = hulp.Text;
            teller3++;
            //bepaal de inhoud van je pdf document (locatie)
            string inhoud = File.ReadAllText(Server.MapPath("pdf.aspx"));
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(hulp1), null);
            //alle elementen van uw pagina in de pdf steken (foreach.... zeer handige functie)
            foreach (var htmlElement in parsedHtmlElements)
                doc1.Add(htmlElement as IElement);
        }
        //nu de pdf gemaakt is gaan we de boel afsluiten:
        doc1.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename= Evaluatierapport.pdf"));
        Response.BinaryWrite(output.ToArray());       
    }


      private void btnSave_Click(object sender, EventArgs e) 
      {
          SaveFileDialog saveFileDialog = new SaveFileDialog();
          saveFileDialog.DefaultExt = "bmp";
          saveFileDialog.Filter = "Bitmap files|*.bmp";
          if (saveFileDialog.ShowDialog() == DialogResult.OK)
          {
              int width = pnlMain.Width;
              int height = panel.Height;
              Bitmap bitMap = new Bitmap(width, height);
              System.Drawing.Rectangle rec = new Rectangle(0, 0, width, height);
              panel.DrawToBitmap(bitMap, rec);
              bitMap.Save(saveFileDialog.FileName);
          }
      }
}

如何在c#中保存面板的内容

我认为你是在动态加载控件:

我有一个列表框,我选择的形式我想加载到面板中的内容

动态添加的控件不会通过回发持续存在。您需要使用相同的id再次添加它们…或者自己筛选视图状态。

[编辑]

看看这些:

  • 了解ASP。. NET视图状态
  • 常见问题:为什么要动态控制
(/编辑)