Monday, March 21, 2011

Tuesday, March 15, 2011

Truncate a text field in Gridview

Call a function from a control that supports databinding events (such as a label).

The example below truncates text where it is found to be greater than 50 in length and adds ... to the end.

The controls refers to a function in the backend by the name of TruncateString.

Source:
<asp:TemplateField HeaderText="Desc2">
<ItemTemplate>

<asp:Label ID="Label1" runat="server" 
Text='<%# TruncateString(DataBinder.Eval(Container.DataItem,"Description").ToString()) %>'>
</asp:Label>

</ItemTemplate>
</asp:TemplateField>

Code:
Protected Function TruncateString(ByVal TheText As String) As String
        If TheText.Length > 50 Then
            Return TheText.Substring(0, 50) & " ..."
        Else
            Return TheText
        End If
    End Function

Monday, March 14, 2011

Hyperlinked image in Gridview

Method 1:
Works, however, it is difficult to format the dimensions of the image.

Source:
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" 
runat="server" 
ImageUrl='<%# Eval("ProductID", "ContentImages/{0}.jpg") %>' 
NavigateUrl='<%# String.Format("~/productdetails.aspx?ProductID={0}", Eval("ProductID")) %>'>
</asp:HyperLink>
</ItemTemplate>

Method 2:

Use seperate Hyperlink control & Image controls - wrap the hyperlink tag around the image control.

<ItemTemplate>

<asp:HyperLink ID="hlkImageLink
runat="server" 
NavigateUrl='<%# String.Format("~/PT_productdetails.aspx?ProductID={0}", Eval("ProductID")) %>'>

<asp:Image ID="imgProductImage
runat="server" 
ImageUrl='<%# Eval("ProductID", "ContentImages/{0}.jpg") %>'
Width="50" Height="50" />
                    
</asp:HyperLink>
                
</ItemTemplate>

Format currency in GridView

Set the DataFormatString property of the Gridview field to:

{0:C}

This will format the DataField at position 0 (in the array) to a currency value.

A decimal value of 99.0000 will be formatted to $99.00





Also see: FormatCurrency Function

Tuesday, March 8, 2011

Referring to Connection String in Web.Config

When instantiating the SqlConnection object:


MyConnString = ConfigurationManager.ConnectionStrings("TheConnectionString").ConnectionString


Where "TheConnectionString" is the name of the connection string in Web.Config.


"SqlConnection Class (System.Data.SqlClient)" ( http://bit.ly/J5ew4y )

Monday, March 7, 2011

Detect Browser (Server side)

"How to determine browser type in server-side code without the BrowserType object in ASP.NET" ( http://bit.ly/hAAFXl )

lblUserAgent.Text = Request.Browser.Browser & " " & Request.Browser.Version

Saturday, March 5, 2011

Casting

"Cheat Sheet - Casting in VB.NET and C# - CodeProject" ( http://bit.ly/eNDEgR )

Wednesday, March 2, 2011

Server.Transfer Vs. Response.Redirect

STOP THE PRESS

"Server.Transfer Vs. Response.Redirect - Developer.com" ( http://bit.ly/e43UFS )
 &
"Server.Transfer vs. Response.Redirect" ( http://bit.ly/h5uxCo )

Tuesday, March 1, 2011

Javascript comments

Javascript single line comment
//

Javascript multi-line comment
/*
This is a
multi line comment
*/

StringBuilder Class

"StringBuilder Class (System.Text)" ( http://bit.ly/iidbFf )

EG:
"Iterate Through The Form Collection In ASP.NET 2.0 - Robert Robbins" ( http://bit.ly/gOFBQ0 )

Status codes in HTTP

"Status codes in HTTP" ( http://bit.ly/fgcm4q )