ASP.NET Project Creation Adding an ASP.NET control

W
Document Sample
scope of work template
							                                                           Review
                                                            ASP = Active Server Pages
                                ASP.NET                       ASP here refers to ASP.NET, not original ASP
                                                            ASP pages have HTML and ASP controls
                Lecture 14                                  ASP controls are members of a class that is
                                                            built for you – can access them from program
                                                            code that is associated with the ASP page
         CS 638 Web Programming                             Controls can raise events / cause postbacks
                                                            Processing of a page has many steps in its
                                                            lifecycle on the server

                                                                       CS 638 Web Programming – Estan & Kivolowitz




ASP.NET Project Creation                                   Adding an ASP.NET control
 Start VS 2005                                              Hover or click on Toolbox
 New->Web Site                                              Click and hold on “Label”
 Select ASP.NET Web Site                                    Drag to desired location in aspx file
 Fill in location                                           (between the “div” statements in this case)
 Hit OK                                                     Change ID of control to “lblTime”
 An “empty” default.aspx page is created and                Debug / Run web page (F5)
 shown.                                                     Say yes to creating a web config setting
                                                            Browser appears with page loaded
             CS 638 Web Programming – Estan & Kivolowitz               CS 638 Web Programming – Estan & Kivolowitz




If IE is not Default Browser                               Debugging an ASP.NET page
 If a browser other than Internet Explorer                  While still viewing page, hit reload (Ctrl-R)
 comes up, do this:                                         Page reloads
   Switch back to VS2005                                    Switch to VS2005
   Hit Shift-F5 (kills currently running debug session)
                                                            Right click to bring up context menu
   Click or hover over Solution Explorer
   Right click on default.aspx
                                                            Select “View code”
   Click on “Browse With”                                   default.aspx.cs comes up
   Select Internet Explorer and set it to default           Put breakpoint (F9) on closing brace of
   Hit OK and rerun page (F5) IE should come up             Page_Load
             CS 638 Web Programming – Estan & Kivolowitz               CS 638 Web Programming – Estan & Kivolowitz
Debugging an ASP.NET page                                 Adding Another Control
 Switch back to browser                                    Looking at default.aspx, place (click) cursor
 Reload page                                               before opening “<“ of the ASP label
 Execution of the page halts at the breakpoint             Click or hover over Toolbox
 moving VS2005 to front                                    Double click on Button
 Continue execution with F5
                                                           Button appears where cursor was positioned
 Browser comes to front
                                                           Change ID to “btnSubmit” and Text to “Get
 Close browser window – VS2005 comes
 back – debugging is terminated                            Time”
 Remove breakpoint (F9 again when on line)                 Run page – should look like this…

            CS 638 Web Programming – Estan & Kivolowitz              CS 638 Web Programming – Estan & Kivolowitz




Page so far                                               Add Event Handler
                                                           Terminate debugging of page
                                                           View default.aspx
                                                           At bottom click “Design”
                                                           A WYSIWYG preview of your page comes up
                                                           Double click on the button
                                                           View switches to default.aspx.cs
                                                           VS2005 adds an event handling stub for you


            CS 638 Web Programming – Estan & Kivolowitz              CS 638 Web Programming – Estan & Kivolowitz




Add Event Handler                                         Modifying Control’s Look
 Enter:                                                    ASP.NET control offer many options for
 lblTime.Text = DateTime.Now.ToShortTimeString();          modifying their look
 Run page (F5)                                             Modifications can be declared right along with
 Click on button                                           the control’s declaration
 Should see:                                               Controls can also reference CSS styles




            CS 638 Web Programming – Estan & Kivolowitz                    CS 638 Web Programming – Estan & Kivolowitz
Modifying a Control’s Look                                            Working with Controls
<asp:Button ID="Button1" runat="server" Text="" />                    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:Button ID="Button2" runat="server" Text=“"                          <asp:ListItem Text="Pickles" Value="1"></asp:ListItem>
  BackColor="AliceBlue"                                                  <asp:ListItem Value="2">Onions</asp:ListItem>
  Font-Bold="True" />                                                    <asp:ListItem>Sesame Seed Bun</asp:ListItem>
<asp:Button CssClass="bstyle" ID="Button3"                            </asp:CheckBoxList>
  runat="server" Text="" />                                           <br />
                                                                      <asp:Button ID="Button1" runat="server" Text="Push Me" />




                        CS 638 Web Programming – Estan & Kivolowitz                       CS 638 Web Programming – Estan & Kivolowitz




Working with Controls                                                 Working with Tables
                                                                      From the aspx file:
                                                                      CheckBoxList1 states:
                                                                      <asp:Table runat="server" ID="tblCBL1" Width="400">
                                                                      </asp:Table>




                        CS 638 Web Programming – Estan & Kivolowitz                       CS 638 Web Programming – Estan & Kivolowitz




Working with Tables                                                   Initializing Controls via Code
tbl.Rows.Clear();                                                       Initializing controls via code is often required
TableRow row = new TableRow();
TableCell cell;                                                            e.g.: loading control contents from database
string[] headings = { "Index", "Text", "Value", "Checked" };            From aspx file:
foreach (string s in headings)
                                                                      <asp:CheckBoxList ID="CheckBoxList1" runat="server">
{
    cell = new TableCell();
                                                                      </asp:CheckBoxList>
    cell.Text = s;
    row.Cells.Add(cell);
}
tbl.Rows.Add(row);


                        CS 638 Web Programming – Estan & Kivolowitz                       CS 638 Web Programming – Estan & Kivolowitz
Initializing Controls via Code                                                Questions…




                       CS 638 Web Programming – Estan & Kivolowitz                                  CS 638 Web Programming – Estan & Kivolowitz




A Server-Based View Control                                                   Control Container and its Items
                                                                              <asp:MultiView runat="Server" ID="MultiView1" ActiveViewIndex="0">
                                                                                 <asp:View ID="View1" runat="server">
                                                                                      Mark up
                                                                                 </asp:View>
 Common in today’s web pages are view or                                         <asp:View ID="View2" runat="server">
                                                                                      More mark up
 paging controls
                                                                                 </asp:View>
 ASP.NET provides a server-based view                                            <asp:View ID="View3" runat="server">
 control                                                                              Even more mark up
                                                                                 </asp:View>
                                                                              </asp:MultiView>


                       CS 638 Web Programming – Estan & Kivolowitz                                  CS 638 Web Programming – Estan & Kivolowitz




ASP:Buttons Have New
“Command” Semantics                                                           Master Pages
 You have seen:                                                                 To create pages with a common look, use
 <asp:Button ID= “id" runat="server" Text=“text" OnClick= "EventHandler" />
                                                                                Master Pages
 There is also a “command based” event                                          The master page aspx file specifies the
 handler that permits “reserved” actions such                                   markup of the overall look of the page
 as “PrevView” and “NextView” for View
 controls                                                                       A placeholder is included for the markup of
                                                                                each specific or subordinate page
 What’s the benefit?
 You can better encapsulate implementation                                      Both the master and the subordinate have
 when creating controls for others to use                                       implementation (code) files too and go
                                                                                through their own page lifecycles
                       CS 638 Web Programming – Estan & Kivolowitz                                  CS 638 Web Programming – Estan & Kivolowitz
Master Pages                                                       Master Pages
                                                                     Master page includes:
                                                                     <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
                                                                     </asp:contentplaceholder>

                                                                     Subordinate pages include:
  Master page has different Page directive                         <asp:Content ID="Content1"
<%@ Master Language="C#" AutoEventWireup="true"                       ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>         … markup …
  Subordinate page’s Page directive also                           </asp:Content>

  changed:
… MasterPageFile="~/MasterPage.master" …

                     CS 638 Web Programming – Estan & Kivolowitz                        CS 638 Web Programming – Estan & Kivolowitz

						
Related docs