Vincent Dagpin
Vincent Dagpin

Reputation: 3611

BasePage in ASP.Net

i want to create a base class page that has all the scripts, link, and icon in it and i have this so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VGD.Client
{
    public class BasePage : Page
    {
        public new string Title { get; set; }

        protected override void FrameworkInitialize()
        {
            Controls.Add(ParseControl("<!DOCTYPE html>"));
            Controls.Add(ParseControl("<html lang='en'>"));

            Controls.Add(ParseControl(head()));
            Controls.Add(ParseControl(upperBody()));

            base.FrameworkInitialize();
        }

        protected override void OnPreRender(EventArgs e)
        {
            Controls.Add(ParseControl(lowerBody()));

            base.OnPreRender(e);
        }

        private string head()
        {
             string _retVal = @"<head id='Head1' runat='server'>                                    
                                    <title>" + Title + @"</title>                                    
                                </head>";

            return _retVal;
        }

        private string upperBody()
        {
            string _retVal = @"<body>
                                <form runat='server'>";

            return _retVal;
        }

        private string lowerBody()
        {
            string _retVal = @"</form>
                            </body>
                        </html>";

            return _retVal;
        }
    }
}

but upon the initialize, it throws an error that Unexpected end of file looking for </form> tag.

i separated the upperBody() and lowerBody() so that the content of Home.aspx will be added in between the upperBody() and lowerBody() upon creating the page.

any help please.

Upvotes: 1

Views: 1610

Answers (2)

Bobskie Cortez
Bobskie Cortez

Reputation: 26

try this:

Page.Header.Controls.Add(Tag("script", _sb.ToString(),
                 new PnAAttribute("type", "text/javascript")));

Upvotes: 1

Karl Anderson
Karl Anderson

Reputation: 34834

This is what you used to have to do before master pages existed.

Use a master page and you can still add whatever custom logic you want in the code-behind, but still reap the benefits of being able to structure your page with actual HTML, instead of having to write those HTML snippets you had in your example, like this:

protected override void FrameworkInitialize()
{
    Controls.Add(ParseControl("<!DOCTYPE html>"));
    Controls.Add(ParseControl("<html lang='en'>"));

    Controls.Add(ParseControl(head()));
    Controls.Add(ParseControl(upperBody()));

    base.FrameworkInitialize();
}

No need to re-invent the wheel here.

UPDATE:

To store scripts that are available to the master page and any content pages, then use this:

<asp:ScriptManager runat="server">
    <Scripts>
        <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=272931&clcid=0x409 --%>
        <%--Framework Scripts--%>

        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="jquery" />
        <asp:ScriptReference Name="jquery.ui.combined" />
        <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
        <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
        <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
        <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
        <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
        <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
        <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
        <asp:ScriptReference Name="WebFormsBundle" />
        <%--Site Scripts--%>

    </Scripts>
</asp:ScriptManager>

Upvotes: 1

Related Questions