[英]C# How Do I Load an image into a code-behind generated table?
我正在使用asp.net/c#创建要下载的PDF文件。 通过创建一系列载入到div创建的HTMLGenericControl中的HTMLTable,这些工作全部在后面的代码中完成。 然后将该div转换为字符串,然后传递给生成PDF文件的函数。 下面是一个代码示例:
//This dataset contains the student's name, age and grade, want it for all sections
List<studentExportPersonal> lsep = sp.getExportPersonal(exportStudents);
//Page Header containg Grade, Student Name, and Age
HtmlGenericControl div = new HtmlGenericControl("div");
foreach (studentExportPersonal sep in lsep)
{
foreach (string sec in secs)
{
HtmlTable identityTable = new HtmlTable();
div.Controls.Add(identityTable);
identityTable.Width = "100%";
identityTable.Border = 0;
identityTable.Style.Add("margin-bottom", "20px");
HtmlTableRow identityTR = new HtmlTableRow();
identityTable.Rows.Add(identityTR);
HtmlTableCell tdGrade = new HtmlTableCell();
identityTR.Cells.Add(tdGrade);
tdGrade.Style.Add("font-size", "30px");
tdGrade.Align = "center";
tdGrade.RowSpan = 2;
tdGrade.InnerText = sep.si.studentGrade;
HtmlTableCell tdStudentName = new HtmlTableCell();
identityTR.Cells.Add(tdStudentName);
tdStudentName.Style.Add("font-size", "30px");
tdStudentName.Align = "center";
tdStudentName.InnerText = sep.si.studentName;
HtmlTableCell tdStudentAge = new HtmlTableCell();
identityTR.Cells.Add(tdStudentAge);
tdStudentAge.Style.Add("font-size", "30px");
tdStudentAge.Align = "center";
tdStudentAge.RowSpan = 2;
tdStudentAge.InnerText = sep.si.studentAge + " Years Old";
HtmlTableRow identityTR1 = new HtmlTableRow();
identityTable.Rows.Add(identityTR1);
HtmlTableCell tdPic = new HtmlTableCell();
identityTR1.Cells.Add(tdPic);
Bitmap studPic;
if (HttpContext.Current.Request.IsLocal)
studPic = (Bitmap)System.Drawing.Image.FromFile(@"C:\studentPics\" + sep.si.studentPicID, true);
else
studPic = (Bitmap)System.Drawing.Image.FromFile(@"E:\Website\Cascade\studentPics\" + sep.si.studentPicID, true);
}
}
export dmc = new export();
var sb = new StringBuilder();
div.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string s = sb.ToString();
dmc.exportPDF("Student Profiles", "Portrait", s);
在foreach循环的底部,您可以看到我正在创建一个位图变量,我的问题是,如何将其加载到HTMLTableCell变量中?
您可以将图像添加到单元格中,就像将单元格添加到行中一样。
HtmlImage image = new HtmlImage();
image.Src ="/studentPics/" + sep.si.studentPicID;
tdPic.Controls.Add(image);
但是,当我查看您的代码时,我认为GridView是更好的选择。 您可以将List直接绑定到GridView。
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
if (!Page.IsPostBack)
{
GridView1.DataSource = lsep;
GridView1.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.