Quick Reference of webForm front-end frameworks and webform frameworks

Source: Internet
Author: User

Quick Reference of webForm front-end frameworks and webform frameworks

The disadvantage of Html files is that they cannot be reused. MVC can be used in _ Layout. in cshtml, The js and css files (such as jq and bootstrap) required for each page are referenced, which makes webform a little more troublesome.

WebForm needs to derive a parent class BasePage for the page, and then add css and js files to the Header of each page in BasePage. Of course, you can also use the js method, such as creating a new js file and then writing document in it. write ("<script src = 'js address'>") is referenced, but the disadvantage is that js needs to be configured on each page, which is troublesome, it is more convenient to write in the class.

Below is the code for adding dynamic js and css.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Utility.WebForm
{
    public class HtmlControl
    {
        /// <summary>
        /// Register js
        /// </ summary>
        /// <param name = "page"> </ param>
        /// <param name = "url"> </ param>
        public static void RegJs (Page page, string [] url)
        {
            foreach (var item in url)
            {
                HtmlGenericControl htmlGenericControl = new HtmlGenericControl ();
                htmlGenericControl.TagName = "script";
                htmlGenericControl.Attributes.Add ("type", "text / javascript");
                htmlGenericControl.Attributes.Add ("src", item);
                page.Header.Controls.Add (htmlGenericControl);
            }
        }

        /// <summary>
        /// Register Css
        /// </ summary>
        /// <param name = "page"> </ param>
        /// <param name = "url"> </ param>
        public static void RegCss (Page page, string [] url)
        {
            foreach (var item in url)
            {
                HtmlLink htmlLink = new HtmlLink ();
                htmlLink.Href = item;
                htmlLink.Attributes.Add ("type", "text / css");
                htmlLink.Attributes.Add ("rel", "Stylesheet");
                page.Header.Controls.Add (htmlLink);
            }
        }
    }
}
When calling this in webForm, just configure css and js.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility.WebForm
{
    /// <summary>
    /// The parent class of the basic page class /// </ summary>
    public class PageBase: System.Web.UI.Page
    {

        #region Static resource address
        public string [] Css {get; set;}
        public string [] Js {get; set;}
        #endregion

        /// <summary>
        /// page initialization
        /// </ summary>
        protected override void OnInit (EventArgs e)
        {
            #region Permission detection
            //. . .
            #endregion

            #region Set the common page style
            HtmlControl.RegCss (this, Css);
            HtmlControl.RegJs (this, Js);
            #endregion
        }
    

        public PageBase ()
        {
            Css = new string [] {
                "/Content/bootstrap/bootstrap.min.css",
                "/Content/bootstrap/bootstrap-responsive.min.css",
                "/Content/ligerUI/skins/Aqua/css/ligerui-all.css",
                "/content/comom/formcommon.css"
            };
            Js = new string [] {
                "/Content/Jquery/jquery-1.9.1.min.js",
                "/Content/bootstrap/bootstrap.min.js",
                "/Content/ligerUI/js/ligerui.min.js",
                "/Public/Js/My97DatePicker/WdatePicker.js",
                "/content/comom/formcom.js"
            };
        }

        //. . . Other codes. . .
    }
}
    
 Need to explain:

HtmlGenericControl class
As long as we add runat on the html tag, he will generate a htmlGenericControl class.

In the .aspx file we define a div as a server control

<div runat = "server" id = "div1" title = "Site Management">
Then the .aspx.designer.cs file will automatically add such tags to us

        /// <summary>
        /// div1 control.
        /// </ summary>
        /// <remarks>
        /// Automatically generated fields.
        /// To modify, move the field declaration from the designer file to the code-behind file.
        /// </ remarks>
        protected global :: System.Web.UI.HtmlControls.HtmlGenericControl div1;
So we can also manually define htmlGenericControl and add it to another server control, such as header.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.