Helper Functions

One of the most common questions on the net about ASP.Net, when coming from a Classic ASP background, is how to handle situations in which conditional programming is necessary. For instance, in Classic ASP, if you had this code:

If recordset("fieldName") <> "" Then
 ' do this
Else
 ' do this
End if

Since, in ASP.Net, we do not have the sequential code, coming throught the page, like it did with Classic ASP, sometimes we’ll have a label or textbox (or whatever), with the same field information which would have been previously contained in‘recordset(“fieldName”)’

.

To handle this, we would create a helper function, in our code section. So, in this case, let’s call the Function ‘CheckItem’. In the tag, we’d start with something like this:

<asp:Label ID="Label1" Text='<%# Container.DataItem("fieldName") %>' Runat="server" />

Next, we’ll add the code for the function:

<asp:Label ID="Label1" Text='<%# CheckItem(Container.DataItem("fieldName")) %>' Runat="server" />

Then, in our code section, we’d create the ‘CheckItem’ function:

Public Function CheckItem(ByVal sItem as String) as String
If sItem<>"" then
	Return sItem
Else
	Return "n/a"
End If

This is all there is to it. When it’s time for the text in Label1 to be shown on the web page, first, the helper function, ‘CheckItem’ is run against the text which is designated. If that item does not equal an empty string, then that item is presented on the page. If not, ‘n/a’ is presented.

This is a really simple example of the helper function, but any scenario you need, for presenting your text like this can be handled in just this manner.

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!