问题描述
我试图创建Asp.Net MVC模板化控件。通过模板控制,我的意思是接受标记像这样输入的控制:
<%Html.PanelWithHeader()
.HeaderTitle(我的头)
.Content(()=>
{%GT;
<! - 用于没有特别的理由UL - >
< UL>
<李>将样品与LT; /李>
<李>将第二项< /李>
< / UL>
<%})渲染(); %GT;
请注意:是的,这是非常相似的所以我花了快看看源$ C $ C。
他们创造从的HtmlHelper实例ViewContext向HtmlTextWriter。当他们写它,它写入到该页面。
在code变为:
//扩展的HtmlHelper
公共静态PanelWithHeaderControl PanelWithHeader(此的HtmlHelper助手)
{
HtmlTextWriter的作家= helper.ViewContext.HttpContext.Request.Browser.CreateHtmlTextWriter(helper.ViewContext.HttpContext.Response.Output); 返回新PanelWithHeaderControl(作家);
}公共类PanelWithHeaderControl
{
私人HtmlTextWriter的作家; 私人字符串headerTitle; 私人诉讼getContentTemplateHandler; 公共PanelWithHeaderControl(HtmlTextWriter的作家)
{
this.writer =作家;
} 公共PanelWithHeaderControl HeaderTitle(字符串headerTitle)
{
this.headerTitle = headerTitle; 返回此;
} 公共PanelWithHeaderControl内容(动作getContentTemplateHandler)
{
this.getContentTemplateHandler = getContentTemplateHandler; 返回此;
} 公共无效渲染()
{
writer.Write(< DIV CLASS = \\面板与头\\>< DIV CLASS = \\头\\>中+ headerTitle +< / DIV>< DIV CLASS = \\内容模板\\>中); getContentTemplateHandler(); writer.Write(< / DIV>< / DIV>中);
}
}
*我知道,code是一个烂摊子
I'm trying to create a templated control with Asp.Net MVC. By templated control, I mean a control that accepts markup as input like so:
<% Html.PanelWithHeader()
.HeaderTitle("My Header")
.Content(() =>
{ %>
<!-- ul used for no particular reason -->
<ul>
<li>A sample</li>
<li>A second item</li>
</ul>
<% }).Render(); %>
Note: Yes, this is very similar to how Telerik creates its MVC controls, I like the syntax.
Here's my PanelWithHeader code:
// Extend the HtmlHelper
public static PanelWithHeaderControl PanelWithHeader(this HtmlHelper helper)
{
return new PanelWithHeaderControl();
}
public class PanelWithHeaderControl
{
private string headerTitle;
private Action getContentTemplateHandler;
public PanelWithHeaderControl HeaderTitle(string headerTitle)
{
this.headerTitle = headerTitle;
return this;
}
public PanelWithHeaderControl Content(Action getContentTemplateHandler)
{
this.getContentTemplateHandler = getContentTemplateHandler;
return this;
}
public void Render()
{
// display headerTitle as <div class="header">headerTitle</div>
getContentTemplateHandler();
}
}
This displays the ul
, but I have no idea how to display custom code within my Render method.
I have tried using the HtmlHelper with no success. I have also tried overriding the ToString method to be able to use the <%=Html.PanelWithHeader()...
syntax, but I kept having syntax errors.
How can I do this?
It turns out that the Telerik MVC extensions are open-source and available at CodePlex so I took a quick look at the source code.
They create an HtmlTextWriter from the ViewContext of the HtmlHelper instance. When they write to it, it writes to the page.
The code becomes:
// Extend the HtmlHelper
public static PanelWithHeaderControl PanelWithHeader(this HtmlHelper helper)
{
HtmlTextWriter writer = helper.ViewContext.HttpContext.Request.Browser.CreateHtmlTextWriter(helper.ViewContext.HttpContext.Response.Output);
return new PanelWithHeaderControl(writer);
}
public class PanelWithHeaderControl
{
private HtmlTextWriter writer;
private string headerTitle;
private Action getContentTemplateHandler;
public PanelWithHeaderControl(HtmlTextWriter writer)
{
this.writer = writer;
}
public PanelWithHeaderControl HeaderTitle(string headerTitle)
{
this.headerTitle = headerTitle;
return this;
}
public PanelWithHeaderControl Content(Action getContentTemplateHandler)
{
this.getContentTemplateHandler = getContentTemplateHandler;
return this;
}
public void Render()
{
writer.Write("<div class=\"panel-with-header\"><div class=\"header\">" + headerTitle + "</div><div class=\"content-template\">");
getContentTemplateHandler();
writer.Write("</div></div>");
}
}
*I know, the code is a mess
这篇关于我怎样才能创建Asp.Net MVC模板化控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!