Saturday, June 2, 2012

Using Master Pages in ASP.NET 4

Master Pages


Master pages contain a different page directive from normal pages.  Instead of @Page, masterpages have @Master.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

The Master page must have a contentplaceholder tag, where the pages deriving from this page will be inserted.

<asp:ContentPlaceHolder ID="MainContent" runat="server"/>

Content Pages


When creating a new page, you can derive from a selected Master Page by ticking 'Select master page'. After clicking 'Add', you will be prompted to select the masterpage from your website.

Adding a web page with master page
You can also do this by adding the following attribute in the @Page directive of the content page:

<%@ Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>

This can also be done throughout the website by defining the masterpage in the pages element in the Web.config file. Any content pages that do not have the contentplaceholder will not apply the master page.

<system.web>
    <pages masterPageFile="~/Site.master" />
  </system.web>

You can also change the page title from the content page by using the Title attribute in the Page directive.

The content page must contain a content control.  The markup written in this control will eventually be rendered in the contentplaceholder (with the same id) of the masterpage. You can have multiple content controls in a content page, as long as there are the same number of contentplaceholders in the masterpage

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <p>
        Put content here.
    </p>
</asp:Content>

Note that the content place holder cannot be nested in other controls or tags or otherwise you will get the following error.  Same happens if there is no content placeholder in a content page.

Content controls have to be top-level controls in a content page or a nested master page that references a master page.


Note: Setting the EnableViewState property on the content page to true but setting the same property
to false in the master page, will result in having view state disabled because the setting on the
master page takes priority.

Getting controls from the Master Page

In order to get a control inside the master page from the content page you can use the FindControl method of the Master page property:

this.Master.FindControl("HeadLoginView") as LoginView;

Getting data from the Master Page

If you need a property from the master page, you will need to reference the master page in the content page by using the @MasterType directive

<%@ MasterType VirtualPath="~/Site.master" %>

Then from the content page, we can access the property that we need

lblUser.Text = this.Master.CurrentUser;

Changing Master Page dynamically


protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = String.Format("~/{0}.master", "Site2");
}

Wednesday, May 16, 2012

T-SQL: Select node value from xml column

SQL Code:

Column name is wau_xml

SELECT wau_xml.value('(/Document/VersionNo)[1]', 'int') FROM Table


XML in column:

<Document>

<VersionNo>1</VersionNo>

</Document>

Thursday, April 19, 2012

Error: A connection was successfully established with the server, but then an error occurred during the pre-login handshake

The local machine suddenly started giving me the error

A connection was successfully established with the server, but then an error occurred during the pre-login handshake

In order to solve it, try the following


  1. Clean your Visual Studio solution
  2. Rebuild project
  3. Reset IIS
  4. Run the project again
This did the trick for me

Friday, April 13, 2012

AppendDataboundItems for ASPxCombobox

In order to mimic the 'AppendDataBound' feature found in normal ASP.NET comboboxes for devexpress and add items to a bound control, you need to use the databound event to insert the new values if you need a server side solution

protected void myComboBox_DataBound(object sender, EventArgs e)
        {
            myComboBox.Items.Add("All", "0").Index = 0;
            myComboBox.SelectedIndex = 0;
        }

You can also do it at client side by using the following code

comboBox.ClientSideEvents.Init = "function(s, e) {s.InsertItem(0, '(ALL)', '');}";



LINQ SubmitChanges() not working

Just a small note:

The problem was because the table did not have a primary key set.  The DataContext class requires a primary key to update

ASP.NET: Get/Set value from/to cookies

string cookieName;
private int CookieValue
    {
        get
        {
            int _intValue =0;
            if(Request.Cookies[cookieName] != null)
                int.TryParse(Request.Cookies[cookieName].Value, out _intValue);

            return _intValue;
        }
        set
        {
            if(Request.Cookies[cookieName] != null)
                Response.Cookies.Add(new HttpCookie(cookieName, Convert.ToString(value)));
            else
                Response.Cookies["cookieName"].Value = Convert.ToString(value);
        }
    }

Thursday, April 12, 2012

Batch file to launch applications with delay


Here is the code.  10 is the delay in seconds


ping –n 10 127.0.0.1>nul
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"
start "" "C:\Program Files\Internet Explorer\iexplore.exe"


Short and simple :)