System.Web.UI.Control 中 HtmlTextWriter 的替代品?

发布于 2024-08-24 20:06:06 字数 1076 浏览 5 评论 0原文

您可以在 Web 应用程序用户控件中使用 FluentHTML 或其他替代方案吗?

您能否将标记文件像 System.Web.UI.UserControl 一样放入 System.Web.UI.Control 中,但没有 Page 对象的负担?

我想减少以下代码的行数:

writer.Write(SacrificalMarkupPage.GenerateControlMarkup(new DialogConfirm()));

writer.AddAttribute("class", string.Format("ajaxResponse {0}", action));
writer.RenderBeginTag("div");

writer.RenderBeginTag("h4");
writer.WriteEncodedText("Delete selected item?");
writer.RenderEndTag(); //h4

var queryString = new HttpQueryString
  {
    {"confirmed", "true"},
    {"src", urlReferer.PathAndQuery}
  };

writer.AddAttribute("href", queryString.MakeQueryString(url).PathAndQuery);
writer.AddAttribute("class", "delete");
writer.RenderBeginTag("a");
writer.WriteEncodedText("Yes");
writer.RenderEndTag(); //a.delete

writer.WriteEncodedText(" | ");

writer.AddAttribute("href", urlReferer.PathAndQuery);
writer.AddAttribute("class", "back");
writer.RenderBeginTag("a");
writer.WriteEncodedText("No");
writer.RenderEndTag(); //a.back

writer.RenderEndTag(); //div.ajaxResponse.ACTION

谢谢

Can you use FluentHTML or other alternatives in an web application user control?

Can you put a markup file in a System.Web.UI.Control like the System.Web.UI.UserControl but without the burden of the Page object?

I would like to reduce the number of lines of the following code:

writer.Write(SacrificalMarkupPage.GenerateControlMarkup(new DialogConfirm()));

writer.AddAttribute("class", string.Format("ajaxResponse {0}", action));
writer.RenderBeginTag("div");

writer.RenderBeginTag("h4");
writer.WriteEncodedText("Delete selected item?");
writer.RenderEndTag(); //h4

var queryString = new HttpQueryString
  {
    {"confirmed", "true"},
    {"src", urlReferer.PathAndQuery}
  };

writer.AddAttribute("href", queryString.MakeQueryString(url).PathAndQuery);
writer.AddAttribute("class", "delete");
writer.RenderBeginTag("a");
writer.WriteEncodedText("Yes");
writer.RenderEndTag(); //a.delete

writer.WriteEncodedText(" | ");

writer.AddAttribute("href", urlReferer.PathAndQuery);
writer.AddAttribute("class", "back");
writer.RenderBeginTag("a");
writer.WriteEncodedText("No");
writer.RenderEndTag(); //a.back

writer.RenderEndTag(); //div.ajaxResponse.ACTION

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

野生奥特曼 2024-08-31 20:06:06

当然,只需使用 HtmlTextWriter.Write并将 FluentHTML 返回的任何内容传递给它:

writer.Write(
    Html.TextBox(m => m.Chapter.Slug).Class("required").Title("Slug")
);

FWIW,WebForms 方式是使用现有的服务器或 Html 控件并将它们呈现出来。如果您只是将一堆硬编码文本串在一起,您可能应该将其硬编码:

writer.Write(
    @"<div>
         <h4>Delete selected item?</h4>
      </div>"
);

Sure, just use HtmlTextWriter.Write and pass it whatever FluentHTML gives you back:

writer.Write(
    Html.TextBox(m => m.Chapter.Slug).Class("required").Title("Slug")
);

FWIW, the WebForms way would be to use the existing Server or Html controls and render those out instead. If you're just stringing together a bunch of hardcoded text, you should probably just hardcode it:

writer.Write(
    @"<div>
         <h4>Delete selected item?</h4>
      </div>"
);
╰沐子 2024-08-31 20:06:06

我开发了自己的辅助工具,现在我可以这样编写标记:

        using (new Xhtml.BlockTags.Body(writer))
        {
            using (new Xhtml.BlockTags.Div("layout", string.Empty, true, writer))
            {
                using (new Xhtml.BlockTags.Div("header", string.Empty, writer))
                {
                    _header.Render(writer);
                }
                using (new Xhtml.BlockTags.Div("content", string.Empty, writer))
                {
                    _content.Render(writer);
                }
                AjaxStatus.Render(writer);
            }
        }

在本例中,块标签的第一个参数是 ID,第二个参数是类,最后一个参数是对编写器的引用

I developed my own helper tools, now i can write markup this way:

        using (new Xhtml.BlockTags.Body(writer))
        {
            using (new Xhtml.BlockTags.Div("layout", string.Empty, true, writer))
            {
                using (new Xhtml.BlockTags.Div("header", string.Empty, writer))
                {
                    _header.Render(writer);
                }
                using (new Xhtml.BlockTags.Div("content", string.Empty, writer))
                {
                    _content.Render(writer);
                }
                AjaxStatus.Render(writer);
            }
        }

in this example, the first parameter of a blocktag is the ID, the second the class, the last a reference to the writer

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