Category Archives: ASP.NET

Web Services

Published by:

  • A method of making information available in a standardized way that could be accessed by any developer’s application over the Web.
  • Is not an application
  • Methods that can be called over the Internet and that can optionally return data to the calling code.
  • Ideal for exchanging data between different systems.
  • Based on solid, well understood standards
  • Can make it easy to exchange data between different platforms
  • Enables client-side pages to talk to web service on different domain
  • Can be added to the application in two ways:
    • Created and exposed to the Web, to share with other developers and other applications.
    • Can be found on the Web and added to your own application.

4 Stage Process of a Web Service

  • The client calls web service over a protocol.
  • The client sends the method to the server, which contains instructions on what is needed from the web service.
  • The server returns values and or an acknowledgement that it has received the method.
  • The client gets the results and acts on the received information

Calling the Web Service

Request sent to the web service contains:-

  • The web service’s URL
  • The fact that it is an HTTP request
  • The amount of information being sent
  • The type of document you want back from the web server
  • Information about the client
  • The date of the request
  • The parameters that you want to send to the web service.

Serialization

Browser collects the information from the form and wraps it up in a document ready for transmission. This is called serialization.

Componentization

Published by:

  • Process of breaking down the design of an application into a series of objects (components) – these components are self-contained parcels of code that have specific functions and are called by the application only when needed.
  • Separate code and content – Code-Behind
  • Separate data from code – data from external database sources
  • Reuse code – User controls can be designed and used again in other web sites.

Why Componentization Important?

  • Saves design and build time of an application
  • Improves management of the application
  • Makes it easier for others to understand our program

Strategy 1 – Separation of Code from Content

  • The code of the page should be separated from the style and design of the page.
  • Designers involve in the interface and programmers responsible with the back-end work.
  • Single-file scenario – Actual code (within <head> tags) exists in the same file where HTML code and ASP.NET controls.

Compilation of Code in ASP.NET

  • When you submit your Web Form to the server, the Web Form and ASP.NET pages have to be translated into a language the server can understand.
  • Code can be compiled in two ways
    • Pre-Runtime Compilation
    • Full Runtime Compilation
  • App_Code Folder
    • any code you place in the App_Code folder will automatically compile at runtime.
    • Use for any code except code-behind code.

Strategy 2 – Data Componentization

  • Data can be accessed through two mechanisms:-
    • Direct access via the data source controls (typical client-server model)
    • Indirect access via the ObjectDataSource control (Three-tier model)

Data Layers

  • Two-Tier Applications
    • Browser is the first tier – Deals with rules about the business or application logic and user interface.
    • Database is the second tier – Data retrieval and manipulation
    • Client-server application
  • Three-Tier Applications
    • Business rules are separate from the client and the database.
    • Three layers: Interface, business rules/application logic, database.
  • ObjectDataSource Control
    • Connects data bound controls to separate objects in the application
    • Does not tie data aware controls directly to a database

Componentization Strategies 3 – User Controls

  • Allows the designer to create controls that can be reused in other pages or other sites.
  • Encapsulate markup, controls, and code that you need to use again and again.

Similarities of User Controls with .aspx Pages

  • Have a markup section where you can enter standard markup and server controls
  • Can contain programming logic either inline or with a Code-Behind file.
  • Have access to page-based information like Request.QueryString
  • Raise some of the events that the Page class raises, including Load.

Differences of User Controls with .aspx Pages

  • Have an .ascx extension instead of .aspx.
  • Cannot be requested in the browser directly – cannot be linked
  • Adding the user control to a page for usage.

Adding User Controls to Content & Master Pages

  1. Register the control by adding an @Register directive to the page or control where you want the control to appear.
  2. Add tags for the user control to the page and optionally set attributes: src, tagname, tagprefix.
    &lt;%@ Register src=&quot;~/Controls/Banner.ascx&quot; tagname=&quot;Tagname&quot; tagprefix=&quot;uc1&quot; %&gt;

User Control Caveats

  • All elements in an HTML page need to have unique Ids.
  • ASP.NET runtime can change the client ID attributes of your HTML elements automatically to keep them unique
  • You might have trouble using CSS ID selectors to refer to elements that have been named by the ASP.NET runtime
  • Workaround:-
    • Use the class selectors instead of ID selectors
    • Use the control’s ClientID

ASP.NET 3.5 Displaying & Updating Data

Published by:

Data and Databases

  • Divided into one of three categories:
    1. Relational Data – data organised into tables (Example: MS Access, MS SQL Server, Oracle, MySQL etc.
    2. Tree Structured Data – data residing in tree structures (Example: XML files, Windows Registry, Windows File System)
    3. Data in Miscellaneous Forms – Excel files, text fiels or in proprietary formats.

Relational Databases

  • Divide information into tables
  • The tables contain records (rows).
  • A record (row) represents one instance of the topic of the table. A table contains multiple columns (fields) that organize values by their type.
  • Data accessed using Structured Query Language (SQL)

Eval() and Bind()

  • Two methods exist to perform a connection between one field in the data source control and a rendering mechanism in the data-bound control:
    • Eval() – for reading
    • Bind() – for reading and writing (two way binding)
  • The first argument in the Eval() or Bind() method is the field in the database.
  • A second argument (if used) can be used to format the data. For example, to format the ServiceCharge as a currency, {0:C} is the format string: Eval(“ServiceCharge”, “{0:C}”)
  • The {0} syntax indicates the place to plug in the value that is found in the database ServiceCharge field.
  • The C specifies that the “ServiceCharge” data must be formatted as a currency.

Accessing Images – using Eval to Build the Pathname

  • Image name is retrieved from a database and combined with the path to the images.
    Eval(“Images”,~/Images/Countries/{0})
  • The {0} syntax indicates the place to plug in the value that is found in the database Image field.
  • Used to find and display an actual image associated with a record.