EW Resource

ASP.Net Code Tips

The code tips below apply mainly to Visual Studio but can be used with Expression Web if you are adding code by hand.

 

Referencing Controls inside templates

If you try to reference controls inside FormView templates for instance, you'll find that you get 'not declared' errors.

To reference 'interior' controls you need to use 'FindControl'. For example if we have an upload button inside a FormView template, FormView1, with an id of 'btnUpload', we would do the following (in VB):

Dim upLoadBtn As Button = FormView1.FindControl("btnUpload")

We can then work with this newly defined button as normal.

Adding items to a databound DropDownList

It's common to populate a drop down list from a database table or xml file. You may need to add an initial item that invites the user to select from the list. This can be done on the page or via script by using the AppendDataBoundItem property.

On the page:

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="widgetName" DataValueField="widgetID"
AppendDataBoundItems="true"
>
<asp:ListItem Value="">Select a widget</asp:ListItem>
</asp:DropDownList>

In script (VB)

Dim li As New System.Web.UI.WebControls.ListItem

li.Value = 0
li.Text = "Select a widget"

DropDownList1.Items.Insert(0, li)

DropDownList1.AppendDataBoundItems = True

Going to a page anchor after post-back

Pages always re-load at the top after post-back. If you have results of some kind at the bottom of the page the user may miss them and it would be useful to be able to go to an anchor tag in the results to avoid this happening. There's no built in property which allows you to do this but it's relatively simple to do.

At the top or bottom  of the results as appropriate, place an asp textbox with zero width and a zero width border. Name it txtAnchor or similar.

Then, in your function that generates the results, or in you page load subroutine, put "txtAnchor.focus()" as the final statement.

Now, when the page re-loads, the focus goes to this invisible text box and you've achieved the desired effect.

Changing colours to user defined values

If you set the colour of a GridView row for instance, you would use something like:

GridView1.RowStyle.BackColor = Drawing.Color.White .

Doing this you are limited to the list of available drawing colours. To get over this you need to use:

Drawing.Color.FromArgb(R, G, B)  .

For example:

Drawing.Color.FromArgb(83, 212, 83)

Dynamically Changing cell colours in a GridView

You may need to change the background colour of a GridView cell dynamically, where the colour depends on its value.

In this example the colour is changed depending on the value of a 'Status' field.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.DataRow Then

Dim strStatus = System.Web.UI.DataBinder.Eval(e.Row.DataItem, "status")

If strStatus Is System.DBNull.Value Then

e.Row.Cells(6).BackColor = Drawing.Color.White

Else

Select Case strStatus

Case "Complete"
e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(204, 255, 204)

Case "Cancelled" 
e.Row.Cells(6).BackColor = Drawing.Color.Silver

Case "New" 
e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(255, 204, 153)

Case Else
e.Row.Cells(6).BackColor = Drawing.Color.White

End Select

End If

End If

End Sub

Default messages for empty DataLists and Repeaters

Unlike the GridView, FormView or DetailsView, the DataList has no EmptyDataTemplate. To provide a default message when there is no data, add a label above the DataList with a suitable message and set it's visibility to 'false'.

Then add the following in your Page_Load sub:

If DataList1.Items.Count = 0 Then

lblMessage.visible = True

End If

Similarly for a Repeater:

Add a default message for no data in a label above the repeater and set it's visibility to 'false'. We then need to force the repeaters databind method before we check for data. (This would otherwise occur later in the page lifecycle).

In your Page_Load sub:

Repeater1.DataBind()

If Repeater1.Items.Count = 0 Then

lblMessage.visible = true

End If

Formatting a Repeater DataItem for currency

There are several ways to apply a currency format to a data item in a Repeater, one of the simplest is:

<%#DataBinder.GetPropertyValue(Container.DataItem, "bPrice", "{0:c0}")%>

Here 'bPrice' is the table column and is formatted to local currency with no decimal places. eg £1200 rather than £1200.00