Sunday, February 4, 2007

Set Focus

In ASP.net there are 2 ways to set focus to a textbox. One uses the defaultfocus property of the form tag and the other uses the SetFocus method in the code behind.

1. defaultfocus (web form)

<form id="frmFocus" runat="server" defaultfocus="txtSecond">

2. SetFocus (code behind)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   Me.SetFocus("textbox")
End Sub

Labels:

Friday, December 15, 2006

Replace Text in ASP.net GridView

There are times when you need to replace text displayed in ASP.net GridView with something different than what is in your database.

Use the RowDataBound property of the GridView. In the properties window click on the Events button and than select RowDataBound.

Here is a simple if statement that checks the 2nd column for the text "ASP" and replaces it with the text "Net".

If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(1).Text = "ASP" Then
e.Row.Cells(1).Text = "NET"
End If
End If

You can easily replace the text of one column in the GridView based on another column. In this example we are replacing the text of the 3rd column based on the 1st column.

If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text = "ASP" Then
e.Row.Cells(2).Text = "NET"
End If
End If

Sometimes you need to append text to a GridView column. In this example if the the column contains "NET" we will add "ASP" to it

If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text = "NET" Then
e.Row.Cells(0).Text = "ASP" & e.Row.Cells(0).Text
End If
End If

Labels:

Thursday, December 14, 2006

Enable word wrap in Microsoft Visual Studio 2005

I am accustumed to using Macromedia Adobe Dreamweaver. In Dreamweaver, it is really easy set up word wrapping for your code view: View> Code View Options> Word Wrap

In Microsoft Visual Studio 2005 and in Visual Web Developer 2005 Express Edition the word wrap feature is a bit hidden. Word wrap is really helpful when writing complex ASP.net code.

The word wrap option in Visual Studio is hidden in the All Languages section.

On the Tools menu, select Options.
Open the Text Editor folder
Open the All Languages folder
Select General
Under Settings – select the Word wrap option

Labels: