Software tips

Random programming tips and tricks

Set MasterType to Reference Properties and Controls of the Master Page

clock November 18, 2007 03:59 by author notjordan

Add the following declaration to content pages to allow easy access to master page public properties and controls.
<%@ MasterType VirtualPath="~/MasterPageName.master"%>
This will allow you to reference a public property or control by making a call like “me.master.PropertyName”. Extra code to cast would be needed if you did not specify the MasterType.

MasterType can also be set when using nested master pages, but you will not be able to reference the top master's controls without creating a public property for each.
Setup nested master pages as shown below.

<%@ Master Language="VB" CodeFile="MasterPageInner.master.vb" Inherits="MasterPageInner" MasterPageFile="~/MasterPageOuter.master"%>
<%@ MasterType VirtualPath="~/MasterPageOuter.master"%>

<asp:Content ContentPlaceHolderID="ContentOuter" runat=server>
    <asp:contentplaceholder id="ContentInner" runat="server">
    </asp:contentplaceholder>
</asp:Content>

Content page:

<%@ Page Language="VB" MasterPageFile="~/MasterPageInner.master" AutoEventWireup="false" CodeFile="MasterTypeTest.aspx.vb" Inherits="MasterTypeTest" title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPageInner.master" %>
<%@ Reference VirtualPath="~/MasterPageOuter.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentInner" Runat="Server">
    <asp:Literal ID="LitTest" runat="server"></asp:Literal>
</asp:Content>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Performance Tip-Use StringBuilder

clock November 10, 2007 05:22 by author notjordan

String concatenation is not efficient in .NET because Strings are immutable. Changes made to a String cause a new copy of the String to be created, resulting in unnecessary overhead for each operation and extra garbage waiting to be collected. This can result in very noticeable performance loss especially when looping.
Make use of the StringBuilder class when concatenating a String instead of adding to a String object. There is some overhead when creating a StringBuilder, but this overhead is offset after about five operations. Make a habit of using the StringBuilder class when five or more operations are expected to be done on the String.

Below is a short speed test to see how dramatic of a difference there can be when looping.

Code-behind:

Dim dateStart As DateTime
Dim dateEnd As DateTime
Dim timeDiff As TimeSpan

'Run for regular string concat-------------
Dim strRegular As String = ""
dateStart = Date.Now
For i As Integer = 0 To 15000
strRegular = strRegular + i.ToString
Next
dateEnd = Date.Now
timeDiff = dateEnd - dateStart
Me.LitSRegular.Text = timeDiff.ToString
'------------------------------------------
'Run for stringbuilder---------------------
Dim strbTest As New StringBuilder()
dateStart = Date.Now
For i As Integer = 0 To 15000
strbTest.Append(i.ToString)
Next
dateEnd = Date.Now
timeDiff = dateEnd - dateStart
Me.LitSBuilder.Text = timeDiff.ToString
'------------------------------------------

Front-end:

Regular string concatenation: <asp:Literal ID="LitSRegular" runat="server"></asp:Literal>
<br />
StringBuilder: <asp:Literal ID="LitSBuilder" runat="server"></asp:Literal>

Running the above code will provide similar results to the following:
Regular string concatenation: 00:00:01.8281250
StringBuilder: 00:00:00.0156250

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Avoid using QueueUserWorkItem in ASP.NET

clock October 22, 2007 18:42 by author notjordan

When you have a method to run that can process independent of the page being displayed it is tempting to kick the method off asynchronously with QueueUserWorkItem to not slow down the page as shown below:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
QueueUserWorkItem(AddressOf DoHeavyProcessing, Me.Request.QueryString("i"))
End Sub

Private Sub DoHeavyProcessing(ByVal param As Object)
'do stuff
End Sub

While this is a simple way to do the extra processing without slowing the page load, doing this in ASP.NET does have very bad side-effects. QueueUserWorkItem will queue the method in the same thread pool that ASP.NET uses, limiting the ability of the web server, unhandled exceptions will also cause unexpected results as well.
Avoid this even if your site gets little traffic.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Using Google Site Search on ASP.NET pages

clock October 15, 2007 19:21 by author notjordan

Google "Adsense for Search" is a great way to easily allow visitors to search for content on your site. Not only is this service free, but Google will pay you if visitors click ads displayed on the search results page. Begin by setting up an account at adsense.Google.com, then setup a custom “Adsense for Search” and save the generated code.

Example site search code:

<!-- SiteSearch Google -->
<form method="get" action="http://www.google.com/custom" target="_top">
<table border="0" bgcolor="#ffffff">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/">
<img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a>
</td>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="mydomain.com"></input>
<label for="sbi" style="display: none">Enter your search terms</label>
<input type="text" name="q" size="31" maxlength="255" value="" id="sbi"></input>
<label for="sbb" style="display: none">Submit search form</label>
<input type="submit" name="sa" value="Search" id="sbb"></input>
</td></tr><tr><td>&nbsp;</td><td nowrap="nowrap"><table>
<tr><td>
<input type="radio" name="sitesearch" value="" checked id="ss0"></input>
<label for="ss0" title="Search the Web"><font size="-1" color="#000000">Web</font></label></td>
<td>
<input type="radio" name="sitesearch" value="mydomain.com" id="ss1"></input>
<label for="ss1" title="Search mydomain.com"><font size="-1" color="#000000">mydomain.com</font></label></td>
</tr>
</table>
<input type="hidden" name="client" value="pub-123"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="channel" value="123"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="cof" value="G..."></input>
<input type="hidden" name="hl" value="en"></input>
</td></tr></table>
</form>
<!-- SiteSearch Google -->

This generated code uses a FORM to perform a site search, but this will not work by simply copy/pasting into a ASP.NET site. There are a few ways to get this working, but I prefer to remove the form and build a query string as shown below.

Example modified site search code:

<!-- SiteSearch Google -->
<script type="text/javascript">
function GoogleSearch(q) {
window.location = "http://www.google.com/custom" +
"?q=" + q +
"&domains=mydomain.com" +
"&sitesearch=mydomain.com" +
"&forid=1" +
"&channel=123" +
"&client=pub-123" +
"&ie=ISO-8859-1" +
"&oe=ISO-8859-1" +
"&hl=en";}
</script>
<a href="http://www.google.com/"><img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle" /></a>
<label for="sbi" style="display: none">Enter your search terms</label>
<input type="text" name="q" size="30" maxlength="255" value="" id="sbi" />
<label for="sbb" style="display: none">Submit search form</label>
<asp:Button ID="sbb" OnClientClick="GoogleSearch(q.value);return false;" Text="Search Site" runat=server />
<!-- SiteSearch Google -->

Notice the FORM is now gone and the appropriate values are simply passed in the Query String. The modified code is shorter because I also removed the HTML TABLES and other properties that I did not need. Other properties can be added to further customize the search results page, simply get the example code from Google Adsense and modify as demonstrated above.

To also allow visitors to perform a search by hitting the Enter key after typing in a keyword be sure to wrap the code in a PANEL, setting the default button property to “sbb”.
Ex:

<asp:Panel ID="Panel_goog" runat="server" DefaultButton="sbb">

*Note: In order for Google site search to work your site will need to be indexed by Google. Check to make sure you show up on Google by searching for "site:yourdomain" on Google.com. Submit a site map to Google if not already indexed.

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Disable Session State

clock October 5, 2007 22:55 by author notjordan

Not every web application needs to use Session state. Disable Session state for sites that do not make use of it. The performance improvement may be very minimum, but cutting down extra processing for every request is always good.

Session state can be disabled in the web.config file:
<configuration>
   <system.web>
     <sessionState mode="Off" />
   </system.web>
</configuration>

Session state can also be disabled on individual pages by modifying the "EnableSessionState" page directive on each page. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


ASP.NET 2.0 ViewState Info

clock October 5, 2007 04:56 by author notjordan

ViewState is ASP.NET's way of storing the state of a page to retain values between requests. ViewState is enabled by default for every control, which will add a lot of overhead to requests. Therefore it should be disabled whenever it is not absolutely necessary by setting the “EnableViewState=false” on each control.
ViewState is not limited to storing the state of controls, it can also be used to store and retrieve other values. Call Me.ViewState.Add(“name”, “value”) to store a value in ViewState to be used later on the page.

ViewState Security:
The information stored in the ViewState field can be easily read with a ViewState viewer because it is not encrypted by default. If you plan to store sensitive information in ViewState then you will need to encrypt it. Note that this will add extra overhead on the server, so only use when necessary. Encryption can be enabled for the entire site by setting “<pages viewStateEncryptionMode="Always">” in the web.config, or on individual pages by adding “ViewStateEncryptionMode=Always" in each page directive.

ViewState and Custom Controls:
Developers consuming a custom control have the option of disabling ViewState on that control. Therefore if ViewState is required for the control to work then you should use ControlState instead since this cannot be disabled by the consuming page.

*note: If the ViewState string is too long for a single hidden field (MaxPageStateFieldLength property), it will be split into as many multiple files as needed.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Categories


Tags



Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in