Start and end sections using two controls - Asp.net mvc

In an Asp.net MVC application, I would like to encapsulate an ugly wrapper code (just a literal html opening line and another closing line) that we use so that corners and shadows are compatible with old browsers (we do not use javascript for performance reasons) according with visual design studio.

I would like to put div wrappers in the control so that I can use them on the .aspx view page and not have to look at all the clutter needed to create fancy corners and shadows, but still benefit from viewing the results in the designer.

// open wrapper literal actual content // close wrapper literal

I could not understand how to embed content inside 1 control and have results visible in the designer, as the main pages do, so I test the system using 2 controls containing the html literal.

use case - with one control opening and another closing

<ShadowBoxStart /> //contains div open tags
Hello World. This is actual content with all the nice style divs wrapped around
<ShadowBoxEnd /> //contains div close tags

This displays correctly in all browsers when I launch the application, but the constructor seems to be confused with the fact that one control opens divs and the other closes them and makes them unnecessary. The System.Web.Mvc.ViewUserControls I use contains nothing but literal html, and I replicated the behavior with several different standard div forms and configurations, so I am puzzled by what confuses the designer.

, , . ?

+3
1

HtmlHelper a la the BeginForm, open/close. , 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
}
+5

Source: https://habr.com/ru/post/1725760/


All Articles