[英]HtmlTextWriter solution being added as a WebControl
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.
问题是我无法控制WebControl的位置,它们需要相对于这些控件上方的图表。 How I see it, I need to " compile " the sb.ToString() into a WebControl, which I'll then be able to Controls.Add().
我的看法是,我需要将sb.ToString() 编译为WebControl,然后才能将其添加到Controls.Add()。
I need to go from this working solution: 我需要从这个可行的解决方案中走出来:
private void SetTextBoxes()
私有void SetTextBoxes()
{{
TextBox myBox = new TextBox();TextBox myBox = new TextBox();
System.Web.UI.WebControls.Label myLabel = new System.Web.UI.WebControls.Label();System.Web.UI.WebControls.Label myLabel =新的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. 关键是
Controls.Add()
因为它与覆盖此代码和其他一些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();
}
I solved almost the same task. 我解决了几乎相同的任务。 Here is my results:
这是我的结果:
1) I created small helper method: 1)我创建了小助手方法:
public static void RenderControl(Control control, HtmlTextWriter response)
{
var sWriter = new StringWriter();
var htmlWriter = new HtmlTextWriter(sWriter);
control.RenderControl(htmlWriter);
// Write HTML
response.WriteLine(sWriter);
response.Flush();
}
2) At main code if you want to get rendered Html you are doing somthing like this: 2)在主代码处,如果要渲染HTML,则需要执行以下操作:
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
Control control= new YourControlWithTextBox();
WebControlHelper.RenderControl(control, htmlTextWriter);
return stringWriter.ToString();
}
}
With this implementation you can leave SetTextBoxes() as is. 通过此实现,您可以保留SetTextBoxes()不变。 And you get 2 version of same control.
您将获得2个相同控件的版本。 Here is 2 huge benefits: a)code is still readable and described in asp.net terms.
这有2个巨大的好处:a)代码仍然可读并以asp.net术语进行描述。 b)you can use WebControlHelper.RenderControl everywhere where you need solve simmilar task
b)您可以在需要解决类似任务的任何地方使用WebControlHelper.RenderControl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.