How To Properly Handle MasterPage Updates from Pages

The better way to handle updates to the MasterPage from pages is to use Set/Gets (Properties) within the MasterPage. This makes the solution more resilient and it encapsulates.

// MasterPage

public partial class MasterPage : System.Web.UI.MasterPage
{
public string HeadingLabelText
{
get { return HeadingLabel.Text }
set { HeadingLabel.Text = value }
}

Than to use this new property from within the Page go into aspx source of page and after the page directive, adding another directive: MasterType. Adding this MasterType directive gives VS.NET and ASP.net the intelligence to know where to pick up the appropriate class to which has the property we are interested in (the one we just added: HeadingLabelText), in the Master object within the c# code behind. Without this when you type Master. The appropriate property “HeadingLabelText” won’t be listed in intellisense and ASP.NET won’t recognize the property when it renders the page.

// Page

<%@ Page Title……. %>
<%@ MasterType VirtualPath=”~/MasterPage.master” %>

Than add the following to Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
Master.HeadingLabelText = “Retailer Website – “Products”;
}
Advertisements

C# “prop” code snippet for getter/setter property auto generation

Within Visual Studio, Just type “prop” and hit tab twice and the following stub will be generated automatically generate a property.

 public int MyProperty { get; set; }

You can find the entire list of snippets here:

In Visual Studio 2005,
C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

In Visual Studio 2008,
C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

In Visual Studio 2010,
C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

Global ASP.NET Application Error Logging With Log4Net

In your UI project’s Global.asax.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using log4net;



namespace YourNameSpace
{
    public class Global : System.Web.HttpApplication
    {

        /// <summary>
        /// Gets the log.
        /// </summary>
        /// <value>The log.</value>
        public ILog log
        {
            get { return LogManager.GetLogger("YourAppNameLogger"); }
        }


        protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
        
        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
        {
            log.Fatal("Site Application_Error", Server.GetLastError());
            Response.Redirect("/SiteException.aspx", false);
            System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
            Server.ClearError();
        }


    }
}

Create SiteException.aspx :

<div id="error">
        <img alt="Error Symbol" src="Images/error.png" />
        <p>
            We're sorry but an error was encountered while processing your request.
        </p>
        <p>
            Please try submitting your request again.</p>
        <p>
        </p>
        <p>
            If the problem persists please contact the administrators at 000.000.0000</p>
    </div>

And you can test this in a page by doing the following in a dummy test page:

 public partial class Default : System.Web.UI.Page
    {
        void Page_Load(object sender, System.EventArgs e)
        {
           // test global error handling
            throw (new ArgumentNullException());

        }
    }

It should redirect you to the SiteException.aspx page as an error was thrown.

Adding Controls And Referencing Them In Shared Development Environments

When you install a control (example MS AJAX Control toolkit), the path reference it adds defaults to where you installed the control (dll) which is typically outside of the project.

When you’re dealing with a shared development environment and source control. Add the DLL to the project (perhaps in \Lib) and then add a reference (In project / References) to that DLL now in the project.

That way all parties will be able to make use the control

NUnit Setup In Visual Studio And Example NUnit test case

Once installed, Within Visual Studio, Tools | Nunitit | NUnitit settings

Change NUnit-GUI Executable to install path: C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit.exe

When you want to run an NUnit test, set the NUnit project in VS.NET as startup project and navigate to Tools | Debug Nunit GUI. The Nunit test interface will then load.

From there you can execute your test cases.

Very simple example of NUnit test:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using com.company.product.bl;

namespace com.company.product.nunit
{
    [TestFixture]
    public class TestJHAServiceManager
    {
        [Test]
        public void GetJHANames()
        {
            List<string> strings = JHAServiceManager.GetJHANames("jUnit");
            Assert.IsNotEmpty(strings);
        }


        /// <summary>
        /// Gets the JHA details with invalid id.
        /// Another example where an exception is expected
        /// </summary>
        [Test]
        [ExpectedException]
        public void GetJHADetailsWithInvalidId()
        {
            JobHazardAnalysis jha = new JobHazardAnalysis()
            {
                Id = -25
            };
            jha.LoadDetails();
        }

    }
}

Code Documenting Made Easy – GhostDOC

GhostDoc is a free Visual Studio extension that automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information.

When generating documentation for class derived from a base class or for interface implementation (e.g. .NET Framework or your custom framework), GhostDoc will use the documentation that Microsoft or the framework vendor has already written for the base class or interface.

http://submain.com/products/ghostdoc.aspx