Answers for Day 6

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

True or False: A user control can be used on its own.

A1:

False. It needs to be embedded in an ASP.NET page.

2:

What are the three requirements to turn an .aspx file into a user control?

A1:

Remove all <html>, <form>, and <body> tags; change @ Page directives to @ Control directives; and rename your file to include an .ascx extension.

3:

Build a @ Register directive for a user control with the tag prefix and the names ACMEVBCustomControls and TextWriter, located in the shared.ascx file in a subdirectory named ucontrols.

A1:

The proper syntax is

 <%@ Register TagPrefix="ACMEVBCustomControls" TagName="TextWriter" src="/books/4/226/1/html/2/ucontrols/shared. graphics/ccc.gifascx " %> 
4:

True or False: A custom control must be compiled to be used on an ASP.NET page.

A1:

True.

5:

True or False: A custom control must be derived from the Object class, directly or indirectly.

A1:

False. It must inherit from the System.Web.UI.Control class.

6:

Using VB.NET's property element syntax, create a simple property for a custom control called Color that stores information in viewstate.

A1:

 public property Color as string    Get       Color = ViewState("Color").ToString    End Get    Set       ViewState("Color") = value    End Set end property 
7:

True or False: The LoadControl method can be used to load a custom control dynamically.

A1:

False. LoadControl can load user controls dynamically.

Exercises

1:

Enhance the calculator exercise from Day 2 so that it resembles the Windows calculator. Include memory functions (using Session variables), clear and backspace buttons, and the additional operator buttons. Encapsulate it as a user control. This exercise can get fairly complex, but don't worry! You've learned all of the techniques you need to build it.

Hint: Building the UI should be fairly easy. Just use an HTML table. Also note that much of the functionality is duplicated. For instance, each number button should do the same thing (display a number in a text box), so they can all reference the same event handler. Finally, hidden form fields will help when you're trying to determine which numbers are being manipulated. For instance, a user presses 6, then multiply, then 7. Store the 6 in a hidden field so it's not lost after subsequent form posts.

A1:

The code for the user control, Calculator.ascx, is as follows:

 1:    <script language="VB" runat="server"> 2:       private answer as double 3: 4:       Sub btOperator_Click(Sender as Object, e as EventArgs) 5:          if (hiddenvalue.value <> "" and hiddenvalue.value <> "0") 6:             tbNumber.Text = Operate(Sender.Text, hiddenvalue.value, tbNumber.Text) 7:             hiddenvalue.value = "" 8:             hiddenoperator.value = "" 9:          else 10:             'save old number as hidden input field 11:             ' and clear text box 12:             hiddenvalue.value = tbNumber.Text 13:             hiddenoperator.value = Sender.Text 14:             tbNumber.Text = "0" 15:          end if 16:       End Sub 17: 18:       Sub btSpecOperator_Click(Sender as Object, e as EventArgs) 19:          tbNumber.Text = Operate(Sender.Text, hiddenvalue.value, tbNumber.Text) 20:          hiddenvalue.value = "" 21:          hiddenoperator.value = "" 22:       End Sub 23: 24:       Sub btNumber_Click(Sender as Object, e as EventArgs) 25:          response.write(answer.ToString) 26:          if tbNumber.Text <> "0" then 27:             tbNumber.Text = tbNumber.Text & Sender.Text 28:          else 29:             tbNumber.Text = Sender.Text 30:             answer = nothing 31:          end if 32:       end sub 33: 34:       Sub btClear_Click(Sender as Object, e as EventArgs) 35:          tbNumber.Text = "0" 36:          hiddenoperator.value = "" 37:          hiddenvalue.value = "" 38:       end sub 39: 40:       Sub btBS_Click(Sender as Object, e as EventArgs) 41:          if tbNumber.Text <> "0" then 42:             tbNumber.Text = Left(tbNumber.Text, len(tbNumber.Text)-1) 43:          end if 44:       end sub 45: 46:       Sub btMemory_Click(Sender as Object, e as EventArgs) 47:          select case Sender.text 48:             case "MC" 49:                Session("memory") = "" 50:                Indicator.Text = "" 51:             case "MS" 52:                Session("memory") = tbNumber.Text 53:                Indicator.Text = "M" 54:             case "MR" 55:                tbNumber.Text = Session("Memory") 56:             case "M+" 57: 58:          end select 59:       end sub 60: 61:       Sub btEqual_Click(Sender as Object, e as EventArgs) 62:          if hiddenvalue.value <> "" then 63:             if hiddenoperator.value <> "" then 64:                'operate numbers 65:                tbNumber.Text = Operate(hiddenoperator.value, hiddenvalue.value,  graphics/ccc.giftbNumber.Text) 66:                hiddenvalue.value = "" 67:                hiddenoperator.value = "" 68:                answer = tbNumber.Text 69:             else 70:                'do nothing 71:             end if 72:          else 73:             'do nothing 74:          end if 75:       end sub 76: 77:       private function Operate(operator as string, number1 as string, optional  graphics/ccc.gifnumber2 as string = "1") as double 78:          select case operator 79:             case "+" 80:                Operate = CDbl(number1) + CDbl(number2) 81:             case "-" 82:                Operate = CDbl(number1) - CDbl(number2) 83:             case "*" 84:                Operate = CDbl(number1) * CDbl(number2) 85:             case "/" 86:                Operate = CDbl(number1) / CDbl(number2) 87:             case "sqrt" 88:                Operate = CDbl(Math.sqrt(number2)) 89:             case "1/x" 90:                Operate = CDbl(1 / cdbl(number2)) 91:             case "+/-" 92:                Operate = CDbl(-cdbl(number2)) 93:          end select 94:       end function 95:    </script> 96: 97:    <asp:Panel  runat="server"> 98:       <table width="250"> 99:       <tr> 100:          <td width="100%" colspan="7"> 101:             <asp:textbox  runat=server 102:                text="0" 103:                enabled="false" 104:                width="100%" /> 105:          </td> 106:       </tr> 107:       <tr> 108:          <td align="right" width="100%" colspan="7"> 109:             <asp:Label  text="" runat="server" /> 110:             <asp:button  Text="Backspace" 111:                OnClick="btBS_Click" runat=server width="75"/> 112:             <asp:button  Text="C" 113:                OnClick="btClear_Click" runat=server width="35"/> 114:          </td> 115:       </tr> 116:       <tr> 117:          <td width="32" valign="top"> 118:             <asp:button  Text="MC" 119:                OnClick="btMemory_Click" runat=server 120:                width="35"/> 121:          </td> 122:          <td width="58"></td> 123:          <td width="32"> 124:             <asp:button  Text="7" 125:                OnClick="btNumber_Click" runat=server 126:                width="35"/><p> 127:          </td> 128:          <td width="32"> 129:             <asp:button  Text="8" 130:                OnClick="btNumber_Click" runat=server 131:                width="35"/><p> 132:          </td> 133:          <td width="32"> 134:             <asp:button  Text="9" 135:                OnClick="btNumber_Click" runat=server 136:                width="35"/><p> 137:          </td> 138:          <td width="32"> 139:             <asp:button  Text="/" 140:                OnClick="btOperator_Click" runat=server 141:                width="35"/><p> 142:          </td> 143:          <td width="32"> 144:             <asp:button  Text="sqrt" 145:                OnClick="btSpecOperator_Click" runat=server 146:                width="35"/><p> 147:          </td> 148:       </tr> 149:       <tr> 150:          <td width="32" valign="top"> 151:             <asp:button  Text="MR" 152:                OnClick="btMemory_Click" runat=server 153:                width="35"/> 154:          </td> 155:          <td width="8"></td> 156:          <td width="32"> 157:             <asp:button  Text="4" 158:                OnClick="btNumber_Click" 159:                runat=server width="35"/><p> 160:          </td> 161:          <td width="32"> 162:             <asp:button  Text="5" 163:                OnClick="btNumber_Click" runat=server 164:                width="35"/><p> 165:          </td> 166:          <td width="32"> 167:             <asp:button  Text="6" 168:                OnClick="btNumber_Click" runat=server 169:                width="35"/><p> 170:          </td> 171:          <td width="32"> 172:             <asp:button  Text="*" 173:                OnClick="btOperator_Click" runat=server 174:                width="35"/><p> 175:          </td> 176:          <td width="32"> 177:             <asp:button  Text="%" 178:                OnClick="btOperator_Click" runat=server 179:                width="35"/><p> 180:          </td> 181:       </tr> 182:       <tr> 183:          <td width="32" valign="top"> 184:             <asp:button  Text="MS" 185:                OnClick="btMemory_Click" runat=server 186:                width="35"/> 187:          </td> 188:          <td width="8"></td> 189:          <td width="32"> 190:             <asp:button  Text="1" 191:                OnClick="btNumber_Click" runat=server 192:                width="35"/><p> 193:          </td> 194:          <td width="32"> 195:             <asp:button  Text="2" 196:                OnClick="btNumber_Click" runat=server 197:                width="35"/><p> 198:          </td> 199:          <td width="32"> 200:             <asp:button  Text="3" 201:                OnClick="btNumber_Click" runat=server 202:                width="35"/><p> 203:          </td> 204:          <td width="32"> 205:             <asp:button  Text="-" 206:                OnClick="btOperator_Click" runat=server 207:                width="35"/><p> 208:          </td> 209:          <td width="32"> 210:             <asp:button  Text="1/x" 211:                OnClick="btSpecOperator_Click" runat=server 212:                width="35"/><p> 213:          </td> 214:       </tr> 215:       <tr> 216:          <td width="32" valign="top"> 217:             <asp:button  Text="M+" 218:                OnClick="btMemory_Click" runat=server 219:                width="35"/> 220:          </td> 221:          <td width="8"></td> 222:          <td width="32"> 223:             <asp:button  Text="0" 224:                OnClick="btNumber_Click" runat=server 225:                width="35"/><p> 226:          </td> 227:          <td width="32"> 228:             <asp:button  Text="+/-" 229:                OnClick="btSpecOperator_Click" runat=server 230:                width="35"/><p> 231:          </td> 232:          <td width="32"> 233:             <asp:button  Text="." 234:                OnClick="btNumber_Click" runat=server 235:                width="35"/><p> 236:          </td> 237:          <td width="32"> 238:             <asp:button  Text="+" 239:                OnClick="btOperator_Click" runat=server 240:                width="35"/><p> 241:          </td> 242:          <td width="32"> 243:             <asp:button  Text="=" 244:                OnClick="btEqual_Click" runat=server 245:                width="35"/><p> 246:          </td> 247:       </tr> 248:       </table> 249:    </asp:Panel> 250:    <input type="hidden"  value="" 251:       runat="server" > 252:    <input type="hidden"  value="" 253:    runat="server" > 

The code for the ASP.NET page is as follows:

 1:    <%@ Page language="VB" %> 2:    <%@ Register TagPrefix="TYASPNET" TagName="Calculator" src="/books/4/226/1/html/2/Calculator.ascx" %> 3: 4:    <html><body> 5:       <form runat="server"> 6:          <TYASPNET:Calculator  runat="server"/> 7:       </form> 8:    </body></html> 
2:

Create a meeting planner. It should include a calendar, a text box for user entries, and a label to display the current data. Use AutoPostBack and the TextChanged event when the user enters something in the text box. Store them in session variables so that the user can return to them at a later time. When a note is added, create a label dynamically to provide feedback. Consult Appendix C to learn about the Calendar control's events and methods.

A1:

The code for the user control, planner.ascx is as follows:

 1:    <script language="VB" runat="server"> 2:       Public sub Page_Load(Sender as Object, e as EventArgs) 3:          if not Page.IsPostBack then 4:             Session.Clear 5:             Calendar1.SelectedDate = DateTime.Now 6:             DayLabel.Text = Calendar1.SelectedDate. _ 7:                ToString("dddd, MMMM dd yyyy") 8:          end if 9:       End Sub 10: 11:       Public sub SelectionChanged(Sender as Object, e as EventArgs) 12:          DayLabel.Text = Calendar1.SelectedDate. _ 13:             ToString("dddd, MMMM dd yyyy") 14: 15:          if not (Session(Calendar1.SelectedDate.ToString) is _ 16:             nothing) 17:             dim l as Label = new label 18:             l.Text = Session(Calendar1.SelectedDate.ToString) 19:             DayPanel.Controls.Add(l) 20:          end if 21:       End Sub 22: 23:       Public sub NoteChanged(Sender as Object, e as EventArgs) 24:          if obj.text <> "" then 25:             dim l as Label = New Label 26:             l.text = "<li>" & Sender.Text & "</li>" 27: 28:             DayPanel.Controls.Add(l) 29:             Session(Calendar1.SelectedDate.ToString) = l.text 30:             NoteBox.Text = "" 31:          end if 32:       End Sub 33:    </script> 34: 35:    <table width="100%"> 36:    <tr> 37:       <td width="50%" valign="top"> 38:          <font face="arial"> 39:             <asp:Label  runat="server" 40:                Height="25px" Width="100%" 41:                BackColor="#ddaa66" ForeColor="white" 42:                Font-Bold="true" BorderStyle="groove" /> 43:             <br> 44:             <asp:Panel  runat="server" 45:                backcolor="#ffffff" borderStyle="groove" 46:                Height="225px" > 47: 48:                <asp:Textbox  runat="server" 49:                   OnTextChanged="NoteChanged" 50:                   TextMode="Multiline" 51:                   Rows=5 Width="100%" 52:                   AutoPostBack="true" /><p> 53:             </asp:Panel> 54:          </font> 55:       </td> 56:       <td width="50%" rowspan="2" valign="top"> 57:          <asp:Calendar  runat="server" 58:             OnSelectionChanged="SelectionChanged" 59:             Cellpadding="5" Cellspacing="5" 60:             DayHeaderStyle-Font-Bold="True" 61:             DayNameFormat="Short" 62:             Font-Name="Arial" Font-Size="12px" 63:             height="250px" 64:             NextPrevFormat="ShortMonth" 65:             NextPrevStyle-ForeColor="white" 66:             SelectedDayStyle-BackColor="#ffcc66" 67:             SelectedDayStyle-Font-Bold="True" 68:             SelectionMode="DayWeekMonth" 69:             SelectorStyle-BackColor="#99ccff" 70:             SelectorStyle-ForeColor="navy" 71:             SelectorStyle-Font-Size="9px" 72:             ShowTitle="true" 73:             TitleStyle-BackColor="#ddaa66" 74:             TitleStyle-ForeColor="white" 75:             TitleStyle-Font-Bold="True" 76:             TodayDayStyle-Font-Bold="True" /> 77:       </td> 78:    </tr> 79:    </table> 

The code for the ASP.NET page is as follows:

 1:    <%@ Page Language="VB" debug="true" %> 2:    <%@ Register TagPrefix="TYASPNET" TagName="Planner" src="/books/4/226/1/html/2/planner.ascx" %> 3: 4:    <html><body> 5:       <form runat="server"> 6:          <TYASPNET:Planner  runat="server" /> 7:       </form> 8:    </body></html> 


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

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