我正在使用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);
}
}
}
答案 0 :(得分:3)
通过这个我认为你是动态加载控件:
我有一个列表框,我选择了表格 我想加载到我的面板中。
动态添加的控件不会通过回发持续存在。您需要使用相同的ID重新添加它们...或者自己在viewstate中进行筛选。
[编辑]
看看这些:
[/编辑]