Exposing Properties and Methods to Your ASP.NET Pages

   

I've slipped and included some of what I am going to explain here in the previous example, but I didn't want to make the previous example too plain vanilla. I am trying to get the gears rolling in your head, and one way I like to do that is not giving examples that are so, well, boring when I introduce new concepts.

User controls are, as I've said at least twice if not three times, objects just like everything else in the .NET Framework, and because of this we can use them and their properties and methods in ASP.NET web form pages just like other objects. Here you'll investigate, explore, and uncover ways of using this stuff in your applications.

Properties

There are two different types of properties that are available for use in a User control. There is a public variable type, and there are properties that have explicit Get and Set functions that you can place logical code inside of, just like with the ball object you created in Chapter 2.

When you deal with the public variable type of property in User controls, you're dealing with a property just as you might deal with any server control or object. You can deal with them declaratively or programmatically. Take a look.

Visual Basic .NET uc_simple_property_vb.ascx
<script  language="vb" runat=server>  Public OurLabelsText as String  Sub Page_Load()      OurLabel.Text = OurLabelsText  End Sub  </script>  <asp:Label  runat="server"/> 
C# uc_ simple_property _cs.ascx
<script  language="c#" runat=server>  public String OurLabelsText;  void Page_Load(){     OurLabel.Text = OurLabelsText;  }  </script>  <asp:Label  runat="server"/> 

The first thing highlighted in this User control is the public variable. The public keyword is what makes it available outside of the code block in which it resides, hence exposing it for use in the ASP.NET page in which it's being included. The second thing that's highlighted is that the Text property of OurLabel is set to the value of this public variable. Now look at the ASP.NET web form page.

Visual Basic .NET uc_simple_property_vb.aspx
<%@ page language="vb" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="PublicVariableExample" src="/books/1/582/1/html/2/uc_simple_property_vb. graphics/ccc.gifascx"%>  <script  runat=server>  Sub Page_Load()      If Page.IsPostBack then            OurPublicVariable.OurLabelsText = OurTextBox.Text      End If  End Sub  </script>  <html>  <title>User Controls -  Public Variable</title>  <body>  <form  runat="server">  <table width="200" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Change Variable"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:PublicVariableExample OurLabelsText="It can't be this easy to access a  graphics/ccc.gifpublic variable"  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 
C# uc_simple_property_cs.aspx
<%@ page language="cs" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="PublicVariableExample" src="/books/1/582/1/html/2/uc_simple_property_cs. graphics/ccc.gifascx"%>  <script  runat=server>  void Page_Load(){     if (Page.IsPostBack){         OurPublicVariable.OurLabelsText = OurTextbox.Text;      }  }  </script>  <html>  <title>User Controls -  Public Variable</title>  <body>  <form  runat="server">  <table width="200" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Change Variable"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:PublicVariableExample OurLabelsText="It can't be this easy to access a  graphics/ccc.gifpublic variable"  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 

If you look at the two portions that I've highlighted in the ASP.NET pages, you will see two things. First, and actually farther down in the code, a value is declaratively set for the public variable when the OurLabelsText property of the custom tag is set. Then that value is programmatically changed up in the top of the page in the Page_Load event, where the Page's IsPostBack property is checked to see whether it is true. If it is, the OurLabelsText property is set to the value of the OurTextBox. This property passes the value to the public variable.

Note

Notice that we reference our User control as an object by referring to the ID value we set in the custom tag. In the preceding example, you see the Peter:PublicVariableExample tag has an ID of "OurPublicVariable". Then it and its OurLabelsText property are referenced the way you would any other object by an object.property reference. To reference the Property of OurLabelsText, you would address it as OurPublicVariable .OurLabelsText (Object.Property). If you don't give your custom tag an ID, you won't be able to reference it like an object, and as a matter of fact, you won't be able to reference it at all.


In Figures 5.2 and 5.3, you can see the before and after results of this page's function and manipulation of this public variable.

Figure 5.2. On the initial page load, the public variable is set to the value that is declared in the custom tag.
graphics/05fig02.gif
Figure 5.3. When you post back the page, the public variable is set to the value of the text box programmatically.
graphics/05fig03.gif

You can also manipulate properties in the User controls by creating get and set functions as a full blown property just like when you create properties for objects. You may want to do this to verify which type of data an individual put in a text box (although there are server controls that are much more powerful at this). The following is an example of using the set/get property method of using properties in User controls.

Visual Basic .NET User Control uc_setget_property_vb.ascx
<script  language="vb" runat=server>  private DayOfWeek as String  public Property OurLabelsText as String      get          return OurLabel.Text      end get      set          if (value.Length > 0) then              Select(value.ToLower())                  case "monday"                      DayOfWeek = "First day of the work week"                  case "tuesday"                      DayOfWeek = "Second day of the work week"                  case "wednesday"                      DayOfWeek = "Third day of the work week"                  case "thursday"                      DayOfWeek = "Fourth day of the work week"                  case "friday"                      DayOfWeek = "Last day of the work week"                  case "saturday"                      DayOfWeek = "It's Saturday"                  case "sunday"                      DayOfWeek = "It's Sunday"                  case else                      DayOfWeek = "That doesn't appear to be an actual day."              end Select              OurLabel.Text = DayOfWeek          else              OurLabel.Text = "Please enter something in the textbox."          end if      end set  end property  </script>  <asp:Label  runat="server"/> 
Visual Basic .NET Web Form Page uc_setget_property_vb.aspx
<%@ page language="vb" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="PublicVariableExample" src="/books/1/582/1/html/2/uc_setget_property_vb. graphics/ccc.gifascx"%>  <script  runat=server>  Sub Page_Load()      if (Page.IsPostBack)          SetProperty.OurLabelsText = OurTextbox.Text      end if  end sub  </script>  <html>  <title>User Controls -  Public Variable</title>  <body>  <form  runat="server">  <table width="300" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  Enter the name of a day (ie.Friday)<br>  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Change Variable"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:PublicVariableExample  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 
C# User Control Page uc_setget_property_cs.ascx
<script  language="c#" runat=server>  private String DayOfWeek;  public String OurLabelsText{      get{          return OurLabel.Text;          }      set{          if (value.Length > 0){              switch(value.ToLower()){                  case "monday":                      DayOfWeek = "First day of the work week";                  break;                  case "tuesday":                      DayOfWeek = "Second day of the work week";                  break;                  case "wednesday":                      DayOfWeek = "Third day of the work week";                  break;                  case "thursday":                      DayOfWeek = "Fourth day of the work week";                  break;                  case "friday":                      DayOfWeek = "Last day of the work week";                  break;                  case "saturday":                      DayOfWeek = "It's Saturday";                  break;                  case "sunday":                      DayOfWeek = "It's Sunday";                  break;                  default:                      DayOfWeek = "That doesn't appear to be an actual day.";                  break;              }              OurLabel.Text = DayOfWeek;          }else{              OurLabel.Text = "Please enter something in the textbox.";          }      }  }  </script>  <asp:Label  runat="server"/> 
C# Web Form Page uc_setget_property_cs.aspx
<%@ page language="cs" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="PublicVariableExample" src="/books/1/582/1/html/2/uc_setget_property_cs. graphics/ccc.gifascx"%>  <script  runat=server>  void Page_Load(){      if (Page.IsPostBack){          SetProperty.OurLabelsText = OurTextbox.Text;      }    }  </script>  <html>  <title>User Controls -  Public Variable</title>  <body>  <form  runat="server">  <table width="300" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  Enter the name of a day (ie.Friday)<br>  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Change Variable"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:PublicVariableExample  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 

As you can see in Figure 5.4, the User control works without a hitch. If you look at the ASP.NET web form page, you can see that we check to see whether we are posting back using the Page's IsPostBack property.

Figure 5.4. User controls also allow you to set and get properties just as you can with full featured objects.
graphics/05fig04.gif

In a postback, you set the value of the OurLabelText property to the value of the text box, just like you would with any other object. Then it runs through the if statement to check whether you entered anything in the box. It does so by seeing whether the property has any characters. If you entered anything in the box, the Length property will be greater than 0. What was entered then goes through the Select/switch statement to find out what day it matches, or if it doesn't match one of the cases, it defaults to tell you it didn't match. If the Length of the property returns as 0 (empty text box), then we tell you "Hey put somethin' in the box, willya."

This opens up some very interesting avenues for interaction between your ASP.NET web form pages and your User controls.

Methods

Remember, the User control is an object, and objects can have methods, so it is safe to say that User controls can have methods. And is there a method to my madness or an end to this grief? One can never tell, but we hope against hope that the gods in heaven or the mother of all mankind will be kind and have mercy on our fragile souls that we may once again see the sun rise and be glad that another day has been granted us.

Huh?…whatever.

A User control exposes any public sub or function as a method of a User control object. So what's the difference between using a programmatic property (as we did in the previous example) and a method, you ask? Simple. You get to require multiple parameters being passed to a method instead of just the property's value, and you can overload a method as well. Remember that overloading a method is just a way of allowing different "signatures" or combinations of parameters and parameter types to be sent to a method.

Let's look at an example where you send can send a different number of parameters to a sub/function by overloading the method. Again, a parameter with a set/get can deal with delivering only one value to the User control, which is the value of the parameter. With methods, the number and combinations are limited only by your desire to limit the method.

Visual Basic .NET User Control uc_method_vb.ascx
<script  language="vb" runat=server>  public Sub OurMessage(Message as Integer)      if (Message = 0) then          OurLabel.Text = "You don't want a message"      elseif (Message = 1) then          OurLabel.Text = "You want the default message"          OurOtherLabel.Text = "The default message is BLAHHHH!!!"      elseif (Message = 2) then          OurLabel.Text = "You want your own message"          OurOtherLabel.Text = "But you didn't include a message"      end if  end sub  public Sub OurMessage(Message as Integer,TheMessage as String)      if (Message = 0) then          OurLabel.Text = "You don't want a message"      elseif (Message = 1) then          OurLabel.Text = "You want the default message"          OurOtherLabel.Text = "The default message is BLAHHHH!!! and we ignore your  graphics/ccc.gifmessage"      elseif (Message = 2)          OurLabel.Text = "You want your own message"          OurOtherLabel.Text = TheMessage      end if  end sub  </script>  <asp:Label  runat="server"/><br>  <asp:Label  runat="server"/> 
Visual Basic .NET Web Form uc_method_vb.aspx
<%@ page language="vb" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="MethodExample" src="/books/1/582/1/html/2/uc_method_vb.ascx"%>  <script  runat=server>  Sub Page_Load()      if (Page.IsPostBack) then          if (OurMessage.Text.Length > 0) then              UCMethod.OurMessage(MessageType.SelectedIndex,OurMessage.Text)          else              UCMethod.OurMessage(MessageType.SelectedIndex)          end if      end if  end sub  </script>  <html>  <title>User Controls -  Methods</title>  <body>  <form  runat="server">  <table width="300" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  Select the type of message.<br>  <asp:DropDownList  runat="server">  <asp:ListItem  text="No Message" />  <asp:ListItem  text="Default Message" />  <asp:ListItem  text="My Own Message" />  </asp:DropDownList>  <br>  Enter your message. .  if you dare!!!<br>  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Send Message"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:MethodExample  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 
C# User Control uc_method_cs.ascx
<script  language="c#" runat=server>  public void OurMessage(int Message){      if (Message == 0){          OurLabel.Text = "You don't want a message";      }else if (Message == 1){          OurLabel.Text = "You want the default message";          OurOtherLabel.Text = "The default message is BLAHHHH!!!";      }else if (Message == 2){          OurLabel.Text = "You want your own message";          OurOtherLabel.Text = "But you didn't include a message";      }  }  public void OurMessage(int Message, string TheMessage){      if (Message == 0){          OurLabel.Text = "You don't want a message";      }else if (Message == 1){          OurLabel.Text = "You want the default message";            OurOtherLabel.Text = "The default message is BLAHHHH!!! and we ignore your  graphics/ccc.gifmessage";      }else if (Message == 2){          OurLabel.Text = "You want your own message";          OurOtherLabel.Text = TheMessage;      }  }  </script>  <asp:Label  runat="server"/><br>  <asp:Label  runat="server"/> 
C# Web Form uc_method_cs.aspx
<%@ page language="cs" EnableViewState="false" runat="server"%>  <%@Register TagPrefix="Peter" Tagname="MethodExample" src="/books/1/582/1/html/2/uc_method_cs.ascx"%>  <script  runat=server>  void Page_Load(){     if (Page.IsPostBack){         if (OurMessage.Text.Length > 0){             UCMethod.OurMessage(MessageType.SelectedIndex,OurMessage.Text);          }else{             UCMethod.OurMessage(MessageType.SelectedIndex);          }      }  }  </script>  <html>  <title>User Controls -  Methods</title>  <body>  <form  runat="server">  <table width="300" border="0" cellspacing="0" cellpadding="0">  <tr>  <td>  <div align="center">  Select the type of message.<br>  <asp:DropDownList  runat="server">  <asp:ListItem  text="No Message" />  <asp:ListItem  text="Default Message" />  <asp:ListItem  text="My Own Message" />  </asp:DropDownList>  <br>  Enter your message. .  if you dare!!!<br>  <asp:Textbox  runat="server"/><br>  <asp:Button  text="Send Message"  runat="server"/>  </div>  </td>  <tr>  <td>  <div align="center">  <Peter:MethodExample  runat="server"/>  </div>  </td>  </tr>  </table>  </form>  </body>  </html> 

In the results in Figure 5.5, you can see that you can pass either one or two parameters to the method. The first version is executed when you leave the text box empty, because it triggers the first branch of the if statement contained in your IsPostBack check. The text box length is zero, so you call your method and send a single parameter.

Figure 5.5. Exposing Methods in User controls opens up a lot of possibilities over just manipulating properties.
graphics/05fig05.gif

If there is something in the text box, then the else branch is triggered in the if statement contained in the IsPostBack check. This sends a method call with two parameters to the User control.

Notice that as with our parameters, we call our methods just like any other object by objectname.methodname(parameters) . Multiple parameters are separated by commas in User control methods, as well, like other objects.


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net