HtmlTextWriter 解决方案被添加为 WebControl

发布于 2024-12-03 11:03:24 字数 2053 浏览 1 评论 0原文

我有一个可行的解决方案,但它不满足客户的质量保证要求。问题是我无法控制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-12-10 11:03:24

我解决了几乎相同的任务。这是我的结果:

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)在主代码中,如果您想渲染 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();
        }
    }

通过此实现,您可以按原样保留 SetTextBoxes() 。您将获得同一控件的 2 个版本。这里有两个巨大的好处:
a) 代码仍然可读并以 ASP.NET 术语进行描述。
b)您可以在需要解决类似任务的任何地方使用 WebControlHelper.RenderControl

I solved almost the same task. Here is my results:

1) I created small helper method:

   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:

    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. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文