Html asp.net loaded in the Render pages to dynamically generate or modify

            Sometimes in the project you need to generate a number of different control parameters based on actual pass over to put on the page, this time to rewrite the page Render method to achieve their goals.

The following provides an example to implement, as follows:

// generate html code to be rewritten

private string createInputItem()

{

if (Session["Count"] != null)

{

int icount = int.Parse(Session["Count"].ToString());

StringBuilder sbHtml = new StringBuilder();

#region Input Value

for (int i = 1; i <= icount; i++)

{

sbHtml.Append("<tr align=\"left\"> ");

sbHtml.Append("<td style=\"width:150px\" align=\"left\">");

sbHtml.Append("Schacht " + i.ToString() + " SPN F001 ");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:80px\" align=\"left\">");

sbHtml.Append("<input name=\"txtSPNF001_" + i.ToString() + "\" type=\"text\" style=\"width:80px\" />");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:30px\" align=\"left\">");

sbHtml.Append("øC");

sbHtml.Append("</td>");

sbHtml.Append("</tr>");

sbHtml.Append("<tr align=\"left\"> ");

sbHtml.Append("<td style=\"width:150px\" align=\"left\">");

sbHtml.Append("Schacht " + i.ToString() + " T001");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:80px\" align=\"left\">");

sbHtml.Append("<input name=\"txtT001_" + i.ToString() + "\" type=\"text\" style=\"width:80px\" />");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:30px\" align=\"left\">");

sbHtml.Append("øC");

sbHtml.Append("</td>");

sbHtml.Append("</tr>");

sbHtml.Append("<tr align=\"left\"> ");

sbHtml.Append("<td style=\"width:150px\" align=\"left\">");

sbHtml.Append("Schacht " + i.ToString() + " T007,T009");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:80px\" align=\"left\">");

sbHtml.Append("<input name=\"txtT007_T009_" + i.ToString() + "\" type=\"text\" style=\"width:80px\" />");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:30px\" align=\"left\">");

sbHtml.Append("øC");

sbHtml.Append("</td>");

sbHtml.Append("</tr>");

sbHtml.Append("<tr align=\"left\"> ");

sbHtml.Append("<td style=\"width:150px\" align=\"left\">");

sbHtml.Append("Schacht " + i.ToString() + " VM");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:80px\" align=\"left\">");

sbHtml.Append("<input name=\"txtVM_" + i.ToString() + "\" type=\"text\" style=\"width:80px\" />");

sbHtml.Append("</td> ");

sbHtml.Append("<td style=\"width:30px\" align=\"left\">");

sbHtml.Append("øC");

sbHtml.Append("</td>");

sbHtml.Append("</tr>");

}

#endregion

return sbHtml.ToString();

}

return string.Empty;

}

protected override void Render(HtmlTextWriter writer)

{

StringWriter sw = new StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);

base.Render(htmlWriter);

string strTableTR = createInputItem();

string html = sw.ToString() ;

// will just generated Html code into the page

int startPoint = html.IndexOf("</table>", StringComparison.CurrentCultureIgnoreCase);

if(startPoint > 0)

{

html = html.Insert(startPoint, strTableTR);

}

writer.Write(html);

}

 

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/09/26/2192733.html

Guess you like

Origin blog.csdn.net/weixin_34190136/article/details/93054033