Binary Booyah!

Just another pile of random junk.

Set MasterType to Reference Properties and Controls of the Master Page

clock November 23, 2007 15:59 by author Jordan

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>



Avoid using QueueUserWorkItem in ASP.NET

clock October 28, 2007 04:42 by author Jordan

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.



Using Google Site Search on ASP.NET pages

clock October 15, 2007 17:21 by author Jordan

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.



Disable Session State

clock October 11, 2007 00:55 by author Jordan

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. 



ASP.NET 2.0 ViewState Info

clock October 7, 2007 14:56 by author Jordan

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.



ConnectionString Security Through Encryption

clock October 4, 2007 23:40 by author Jordan

While a web.config file is invisible to normal visitors of a site, you can never by 100% sure that it will not be accessible to hackers. Because a web.config can hold username/password information in the connection string it is always good practice to encrypt this file. Encrypting a web.config in ASP.NET 2.0 is relatively painless since a utility is already provided that uses the RSA encryption algorithm. (aspnet_regiis.exe)

Open the “Visual Studio 2005 Command Prompt”
-to encrypt, run the following command:
   aspnet_regiis -pef "connectionStrings" "C:\inetpub\wwwroot\website”
-to decrypt, run the following command:
   aspnet_regiis -pdf "connectionStrings" "C:\inetpub\wwwroot\website”
Example

*note: changes made to the encrypted connectionString through the Visual Studio tools will update the encrypted string automatically.



ASP.NET error: Could not load file or assemply App_Web

clock September 6, 2007 07:44 by author Jordan

Just another one of those ASP.NET errors that seems to pop up for no reason at all. 

Could not load file or assembly 'App_Web_xxxxxxxx, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

Cause: Something has corrupted the version of the app stored in the .NET temp folder. This seems to occur occasionally when you edit a nested custom control.

QuickFix: Change a setting in the web.config to force the app to recompile. This should get you rolling again, but the problem will happen again eventually.

Unconfirmed fixes: (I'll test more of these if the error pops up again)

1-Configure virus scan software to ignore the app and .NET temp folders. This may help prevent it from happening again.

2-Disable the indexing service. This may help prevent it from happening again.

3-Set "<compilation batch=false" in the web.config file. (My guess is this has no lasting effect over the quickfix above)

4-Clean out the .NET temp directory. 



.NET error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

clock September 5, 2007 11:32 by author Jordan

If you like using .NET TableAdapters then I'd bet you've seen this message more than once:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

This message is (like always) not very helpful in tracking down the problem, but going through the list below should help track down the cause.

Causes:

  • Field size has changed.
  • Bad query.
  • .NET doesnt like you.

Fixes:

  • Field size has changed. This has been the cause a couple of times for me. I'm not the only one modifying the database, so things can change without notice occasionally. Check to make sure the MaxLength property for each field is set correctly in the TableAdapter. If a value is pulled from the database that is too long it can throw the error.
    Note: this can also occur without any changes to the database if your TableAdapter references a Stored Procedure. Stored Procedures do not always communicate the correct MaxLength of fields when building the TableAdapter, so always ensure the MaxLength of each field is correct when using Stored Procedures.
  • Bad query. Run the query in Query Analyser and check to make sure you are not pulling duplicate keys or null values in fields that should not be null.
  • .NET doesnt like you. If all else fails delete the TableAdapter and recreate it. This can be very annoying, but sometimes it is easier than checking each field individually.


Search

Categories


Tags

Disclaimer

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

© Copyright 2012

Sign in