Gery De Smet
Gery De Smet

Reputation: 11

how to save the content of a panel in c#?

I'm working on a project in c # and I have a problem with saving my panel. I have a listbox were I chose the form that I want to load into my panel. But when I want to save my panel it's impossible to read the content of it. After saving my panel i want to convert it to a pdf document but when i can't save it it's impossible to do that. Can somebody help me please ?

    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);
          }
      }
}

Upvotes: 1

Views: 1478

Answers (1)

canon
canon

Reputation: 41675

By this I take it that you're dynamically loading controls:

I have a listbox were I chose the form that I want to load into my panel.

Dynamically added controls don't persist through postback. You'll need to add them again with the same Ids... or sift through the viewstate on your own.

[edit]

Take a look at these:

[/edit]

Upvotes: 3

Related Questions