Friday, January 15, 2010

Class sample code from Jan, 14

First we looked at how to check that the input was numeric. You can use one of two methods. The first is based on an old VB function called "IsNumeric." The second uses the TryParse method of the Double type.

Here is an example of the first way:

Dim mealAmount As Double

If IsNumeric(txtMeal.Text) then
mealAmount = Double.Parse(txtMeal.Text)
Else
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If


Here is an example of the second way. If the text in the text box parses, the value
is assigned to the mealAmount variable

Dim mealAmount as double
Dim result as Boolean

Result=Double.TryParse(txtMeal.Text, mealAmount)

If result=False then
lblError.Text="Please Enter the meal as a numeric amount"
Return
End If


Next we covered simple Ajax in ASP. The first thing you must do is add an scriptmanager object to your web page.

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>


Next you add an Update Panel. Inside the Update panel you put all the controls that you want to post back separately from the main web page. We put in a panel control (not another update panel, just a simple panel that is used to contain other controls. We did it so that we could make the controls invisible and then make them visible with a button click.


<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--this is a button to make the panel visible-->
<asp:Button ID="Button3" runat="server" Text="Show time?" />
<asp:Panel ID="Panel1" runat="server" BackColor="BlanchedAlmond" Visible="false">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" Text="Get Time" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>


I left out the calendar and the other page elements to keep this simple. Here is the code for the two buttons:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Text = TimeOfDay().ToShortTimeString
End Sub

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Panel1.Visible =True
End Sub


Have a good long weekend

No comments:

Post a Comment