Thursday, June 21, 2012

Using Cache in ASP.NET


if (Cache["Name"] != null)

Label1.Text = "Hello," + (string)Cache["Name"];

else

Label1.Text = "Hello, guest! ";


Setting Cache Expiry


Cache.Insert("Name", "Kevin", null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

Thursday, June 14, 2012

How to: Add CSS file in ASP.NET at runtime

Adding CSS file in ASP.NET programatically

HtmlGenericControl si = new HtmlGenericControl();
si.TagName = "link";
si.Attributes.Add("type", "text/css");
si.Attributes.Add("href", "css/module.css");
si.Attributes.Add("rel", "stylesheet");
this.Page.Header.Controls.Add(si);

Tuesday, June 12, 2012

Themes

Creating a theme


Themes should be defined in the special App_Themes folder.

  1. Right-click your website in Solution Explorer, click Add ASP.NET Folder, and then click Theme.
  2. Within the App_Themes folder, you define an individual folder for each theme in your application. Example: Professional or Simple

You can then create skin files or style sheets in each of these theme folders.

Applying the theme


This can be done either on the page level by using the attributes Theme or StyleSheetTheme in the @Page directive.  The difference between Theme and StyleSheetTheme is that StyleSheetTheme sets properties before the page's controls properties are set, while Theme properties are set after the page sets the controls properties.

This means that if in the theme folder, we have set the label colour to Red and in the page we set the label colour to Green, the label will be red if using Theme but green if using StyleSheetTheme.

Or as commonly used, apply it to the whole website through the web.config in the system.web section

<pages Theme=”themeName”>

or

<pages StyleSheetTheme=”themeName”>

Changing theme at runtime programattically


In order to change the theme by code, use the PreInit event to change either the 'Theme' of the page.  If you want that the user changes the theme upon a button click or a change in dropdown, just store the value in  a session variable, and then set the session variable's value to the page.theme in the preinit event

protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = "Simple";
}

To change StyleSheetTheme at runtime, you can't do the same as the above or you will get the error

The StyleSheetTheme property cannot be set, please override the property instead.

In order to apply the StyleSheetTheme at runtime, you must override the property like so:


public override String StyleSheetTheme
{
    get { return "Simple"; }
}
Ideally, use the above code in a base page which all your pages derive from.

Monday, June 11, 2012

Batch file to restart IIS service


Batch file to restart service

net stop "IIS Admin Service"
net start "IIS Admin Service"

You can change the name in the quotes to whatever other service.  If you run it manually, make sure to "Run it as administrator".  If you are using it in a scheduled task, you will need to give it the necessary permissions

[EDIT]
If you get:
The following services are dependent on the IIS Admin Service service. Stopping the IIS Admin Service service will also stop these services.

World Wide Web Publishing Service
HTTP SSL

Do you want to continue this operation? (Y/N)

Then edit the batch file to include another parameter for this choice


net stop "IIS Admin Service" /yes
net start "IIS Admin Service"

[EDIT 2]
Apparently using the above will sometimes stop all websites

Wednesday, June 6, 2012

ASPxSpinEdit validation: number greater than 0

The only way I could find is to use the client-side validation event




or use regex if you find a good expression

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");
}