Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# - help automating some code

Options
  • 04-02-2008 5:15pm
    #1
    Registered Users Posts: 872 ✭✭✭


    Hi,

    I have some code below that writes out the contents of a page on my site. Instead of having this block of code on every aspx page i want to write out content on, i was wondering if i could insert it into my business layer so the same code isnt written out across multiple code behind pages
    webpage w = new webpage();
    
            w.displayPage(2);
    
            this.Title = config.siteName + " " + w.pageTitle;
    
            HtmlMeta metaKeywords = new HtmlMeta();
            metaKeywords.Name = "Keywords";
            metaKeywords.Content = w.pageKeywords;
            this.Header.Controls.Add(metaKeywords);
    
            HtmlMeta metaDescription = new HtmlMeta();
            metaDescription.Name = "Description";
            metaDescription.Content = w.pageDescription;
            this.Header.Controls.Add(metaDescription);
    
            lblPageBody.Text = w.pageBody;
    
            w = null;
    

    so in the future, all my code behind would have is
    w.displayPage(2);
    

    and the rest would be handled in a new method in my business layer.

    Any help would be great, thanks


Comments

  • Registered Users Posts: 413 ✭✭ianhobo


    all you have to do is create another another file.
    In that file have this code as a class method, and just #include this new file in your .aspx code behind file.

    then all you have to do is call the method, either directly if a static method will do, or instantiate the relevant class and call the method at the appropriate time


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    I think you can copy all the below code to your display page method
    this.Title = config.siteName + " " + w.pageTitle;
    
            HtmlMeta metaKeywords = new HtmlMeta();
            metaKeywords.Name = "Keywords";
            metaKeywords.Content = w.pageKeywords;
            this.Header.Controls.Add(metaKeywords);
    
            HtmlMeta metaDescription = new HtmlMeta();
            metaDescription.Name = "Description";
            metaDescription.Content = w.pageDescription;
            this.Header.Controls.Add(metaDescription);
    
            lblPageBody.Text = w.pageBody;
    

    the onyl problem is that from within there "this" is out of scope so you'll need to pass a reference to "this" to your displaypage method meaning your displaypage method would have something like
            myPage.Title = config.siteName + " " + w.pageTitle;
    
            HtmlMeta metaKeywords = new HtmlMeta();
            metaKeywords.Name = "Keywords";
            metaKeywords.Content = w.pageKeywords;
            myPage.Header.Controls.Add(metaKeywords);
    
            HtmlMeta metaDescription = new HtmlMeta();
            metaDescription.Name = "Description";
            metaDescription.Content = w.pageDescription;
            myPage.Header.Controls.Add(metaDescription);
    
            lblPageBody.Text = w.pageBody;
    

    and then in each page you would simply have
            webpage w = new webpage();
            w.displayPage(2,this);
            w = null;
    


  • Closed Accounts Posts: 8 Zan


    another option is to create a base page. put the code you want into the Prerender or the pageload (can actually go into any of the lifecycle methods between onInit and OnRender) in the base page.

    then in your aspx page instead of inheriting from System.Web.UI.Page inherit from your base page.

    No other code is required. and the good thing then is that if you decide to include other code throughout your site. you can just modify the base page accordingly.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Actually better yet use a master page, put all this code in the master then connect your other pages to it.


  • Registered Users Posts: 3,548 ✭✭✭Draupnir


    As was posted two above, the way I would recommend to do any common page level functionality is to use your own base page class which inherits from System.Web.UI.Page

    Then have all pages in your site inherit from your new Page class and have the page load of your base page class execute the code you have posted.

    This is a very quick, easy way to add many page level functions to your entire site, I personally find it very useful for user admin and access functionality.


  • Advertisement
Advertisement