HtmlTextWriter 解决方案被添加为 WebControl
我有一个可行的解决方案,但它不满足客户的质量保证要求。问题是我无法控制 WebControl 的位置,它们需要相对于这些控件上方的图表。在我看来,我需要将 sb.ToString()“编译”为 WebControl,然后我就可以使用 Controls.Add() 了。
我需要从这个工作解决方案开始:
<块引用>私有无效SetTextBoxes()
{
TextBox myBox = new TextBox();
System.Web.UI.WebControls.Label myLabel = 新的 System.Web.UI.WebControls.Label();// ; for (int i = 0; i < _module.Values.Count; i++) { myLabel = new System.Web.UI.WebControls.Label(); myLabel.Text = _module.Values[i].Text.ToString() + ": "; myBox = new TextBox(); myBox.BorderStyle = BorderStyle.None; myBox.ReadOnly = true; myBox.Text = _module.Values[i].Value.ToString("n0"); myBox.Columns = myBox.Text.Length; // } //
}
Controls.Add(myLabel); Controls.Add(myBox); //
关键是 Controls.Add()
,因为它与涵盖此代码和其他一些 WebControl 的模块化系统相关联。
我对解决方案可能是什么样子有一个建议。
private string WriteHtml()
{
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < _module.Values.Count; i++)
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
htmlTextWriter.Write(string.Format("{0}: {1:n0}"
, _module.Values[i].Text
, _module.Values[i].Value));
htmlTextWriter.RenderEndTag(); // td
}
htmlTextWriter.RenderEndTag(); // tr
htmlTextWriter.RenderEndTag(); // table
}
}
//Controls.Add(sb.ToString());
return sb.ToString();
}
I have a solution that works, yet it doesn't meet the QA requirements from the customer. Problem is I can't control the location of the WebControls, they need to be relative to a chart that is above these. How I see it, I need to "compile" the sb.ToString() into a WebControl, which I'll then be able to Controls.Add().
I need to go from this working solution:
private void SetTextBoxes()
{
TextBox myBox = new TextBox();
System.Web.UI.WebControls.Label myLabel =
new System.Web.UI.WebControls.Label();// <table><tr> for (int i = 0; i < _module.Values.Count; i++) { myLabel = new System.Web.UI.WebControls.Label(); myLabel.Text = _module.Values[i].Text.ToString() + ": "; myBox = new TextBox(); myBox.BorderStyle = BorderStyle.None; myBox.ReadOnly = true; myBox.Text = _module.Values[i].Value.ToString("n0"); myBox.Columns = myBox.Text.Length; // <td align="center"> Controls.Add(myLabel); Controls.Add(myBox); // </td> } // </tr></table> }
Key is the Controls.Add()
as it ties into a modular system covering this code and a few other WebControls.
I have a suggestion to what the solution may look like.
private string WriteHtml()
{
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < _module.Values.Count; i++)
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
htmlTextWriter.Write(string.Format("{0}: {1:n0}"
, _module.Values[i].Text
, _module.Values[i].Value));
htmlTextWriter.RenderEndTag(); // td
}
htmlTextWriter.RenderEndTag(); // tr
htmlTextWriter.RenderEndTag(); // table
}
}
//Controls.Add(sb.ToString());
return sb.ToString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了几乎相同的任务。这是我的结果:
1)我创建了小辅助方法:
2)在主代码中,如果您想渲染 Html,您将执行如下操作:
通过此实现,您可以按原样保留 SetTextBoxes() 。您将获得同一控件的 2 个版本。这里有两个巨大的好处:
a) 代码仍然可读并以 ASP.NET 术语进行描述。
b)您可以在需要解决类似任务的任何地方使用 WebControlHelper.RenderControl
I solved almost the same task. Here is my results:
1) I created small helper method:
2) At main code if you want to get rendered Html you are doing somthing like this:
With this implementation you can leave SetTextBoxes() as is. And you get 2 version of same control. Here is 2 huge benefits:
a)code is still readable and described in asp.net terms.
b)you can use WebControlHelper.RenderControl everywhere where you need solve simmilar task