Archive for October, 2008|Monthly archive page

Ways to pass data for asp.net website

  1. ViewState – Maintain data for postback for a page
  2. Context items collection – Simple data pass from 1 page to another. (common use for server farm)
  3. QueryString – put value in the URL
  4. Session – Server store value per user (example: user id)
  5. Application – For the whole web application
  6. Cache – Server storing data among all users (example: the product table)
  7. Cookie – Store information on the user’s pc for a long time (example: track first time visit)
  8. PreviousPage object

…and others

Wilco Syntax Highlighter for ASP.NET Tutorial

I am looking for a good syntax highlighter for my new site. I was searching the forum on ASP.NET and eventually stumble upon Wilco’s syntax highlighter. It is open source which gives you the power to configure it. That site lacks a good tutorial and no examples are provided, so it took me a while to figure out how to customized it. (maybe because I’m still a newbie)

First, open and build Wilco’s project in Visual Studio. Then go to “Wilco.SyntaxHighlighting\bin\Debug” folder to get copy the compiled dll file and paste into the bin folder of your other project. Add reference and browse the dll file. Add it to the toolbox by right clicking the area in the toolbox section, browse the dll file again. You should see 4 more controls in the toolbox. Now just drag and drop it into your web page. Select the mode, language and text.

To customize the look, you can change the style setting in the SyntaxHighlighterTemplates.cs file.

Context.Items vs. ViewState

To pass values from one page to another, Context.Items and ViewState are used most often.

It is important to keep in mind that values stored in the Context.Items collection only last for the current request. If I were to do a postback on the page, the Context item would be lost. Most of the time this is convienient since we only need to store that value between pages and no longer.

To keep track of values between postback, use ViewState. Unlike Context.Items collection that is used to transfer data from one page to another, ViewState can be only used for the same page. It is useful for postback. Also, Context.Items is used very often in the server farm architecture.

It’s common to use this in the page_load function of the page. Check out the example below. Note that the IsPostBack condition below. If that is not there, then the postback of the page will read Context.Items(“id”) again which will be null. This will cause a problem. That is why it is important to have it there.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ProductToShow As Integer
If Not Me.IsPostBack Then
ProductToShow = Context.Items("Id")
Me.ViewState("Id") = ProductToShow
Else
ProductToShow = Me.ViewState.Item("Id")
End If
End Sub

Putting corners using a single image with CSS

  1. The corner image (26 x 26 pixels)
  2. In the CSS file, it only display 13 x 13 pixels of the background image. The background-position property chooses which portion to be displayed.:

    .box .tl, .box .tr, .box .bl, .box .br
    {background-image:url(Images/corners.gif); background-repeat:no-repeat; width:13px; height:13px}
    .box
    .tl {background-position: left top}
    .box
    .tr {background-position: right top}
    .box
    .bl {background-position: left bottom}
    .box
    .br {background-position: right bottom}

  3. In the web page:

    <table cellpadding=”0″ cellspacing=”0″ class=”box”>
    <
    tr>
    <
    td class=”tl”></td>
    <
    td class=”t”></td>
    <
    td class=”tr”></td>
    </
    tr>
    <
    tr>
    <
    td class=”l”></td>
    <
    td class=”c”>
    <
    asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ runat=”server” />
    </
    td>
    <
    td class=”r”></td>
    </
    tr>
    <
    tr>
    <
    td class=”bl”></td>
    <
    td class=”b”></td>
    <
    td class=”br”></td>
    </
    tr>
    </
    table>

Other thought:
I should have used <div> instead of <td>…Now I know more.