[英]Start and End Divs Using Two Controls - Asp.net mvc
在Asp.net MVC应用程序中,我想封装丑陋的包装器代码(只是一个文字的html开始字符串和另一个结束字符串),我们用它来使角落和阴影与旧浏览器兼容(出于性能原因我们不使用javascript )以与视觉工作室设计视图兼容的方式。
我想将包装器div放在一个控件中,这样我就可以在.aspx视图页面中使用它们,而不必查看制作奇特角落和阴影所需的所有混乱,但仍然可以看到结果设计师。
//打开包装器文字实际内容//关闭包装器文字
我无法弄清楚如何在1个控件中注入内容,并在设计器中以母版页的方式显示结果,因此我正在使用包含文字html的2个控件来测试系统。
示例用法 - 一个控件打开,另一个关闭
<ShadowBoxStart /> //contains div open tags
Hello World. This is actual content with all the nice style divs wrapped around
<ShadowBoxEnd /> //contains div close tags
当我运行应用程序时,这在所有浏览器中都能正确呈现,但设计者似乎对一个控件打开div而另一个关闭它们并渲染垃圾这一事实感到困惑。 我正在使用的System.Web.Mvc.ViewUserControls只包含文字html,我用几种不同的标准样式和div配置复制了这种行为,所以我很困惑这是什么让设计师感到困惑。
我想保持解决方案非常简单,因为这主要是一个方便的设置,不值得增加很多复杂性。 有任何想法吗?
您是否考虑过在BeginForm扩展中创建一个HtmlHelper扩展,它允许您执行单个打开/关闭标记。 此扩展返回实现IDisposable的类的对象,并使用Dispose方法生成结束标记。
然后你的HTML看起来像:
<% using (Html.ShadowBoxStart()) { %>
Hello, World!
<% } %>
您可能能够适应的一些代码:
public static class HtmlHelperExtensions
{
/// <summary>
/// Begins a container block using the specified tag. Writes directly to the response. Expected to be used within a using block.
/// </summary>
/// <param name="helper">HtmlHelper object from a View.</param>
/// <param name="tag">The container tag (div, span, hN, etc.)</param>
/// <returns>An MvcContainer that writes the closing tag when it is disposed.</returns>
public static MvcContainer BeginContainer( this HtmlHelper helper, string tag )
{
return BeginContainer( helper, tag, null );
}
/// <summary>
/// Begins a container block using the specified tag. Writes directly to the response. Expected to be used within a using block.
/// </summary>
/// <param name="helper">HtmlHelper object from a View.</param>
/// <param name="tag">The container tag (div, span, hN, etc.)</param>
/// <param name="htmlAttributes">HTML attribute to apply to the tag.</param>
/// <returns>An MvcContainer that writes the closing tag when it is disposed.</returns>
public static MvcContainer BeginContainer( this HtmlHelper helper, string tag, object htmlAttributes )
{
var builder = new TagBuilder( tag );
builder.MergeAttributes( new ParameterDictionary( htmlAttributes ) );
helper.ViewContext.HttpContext.Response.Write( builder.ToString( TagRenderMode.StartTag ) );
return new MvcContainer( helper.ViewContext.HttpContext.Response, tag );
}
}
集装箱类:
/// <summary>
/// Used by the HtmlHelpeExtensions in conjunction with a using block to close
/// a container tag.
/// </summary>
public class MvcContainer : IDisposable
{
protected bool Disposed { get; set; }
protected HttpResponseBase HttpResponse { get; set; }
protected string Tag { get; set; }
public MvcContainer( HttpResponseBase httpResponse, string tag )
{
if (httpResponse == null)
{
throw new ArgumentNullException( "httpResponse" );
}
if (string.IsNullOrEmpty( tag ))
{
throw new ArgumentNullException( "tag" );
}
this.HttpResponse = httpResponse;
this.Tag = tag;
}
/// <summary>
/// Write the closing tag
/// </summary>
public virtual void EndContainer()
{
this.Dispose( true );
}
#region IDisposable Members
/// <summary>
/// Write the closing tag
/// </summary>
public void Dispose()
{
this.Dispose( true );
GC.SuppressFinalize( this );
}
protected virtual void Dispose( bool disposing )
{
if (!this.Disposed)
{
this.Disposed = true;
this.HttpResponse.Write( string.Format( "</{0}>", this.Tag ) );
}
}
#endregion
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.