You are on page 1of 3

Coding techniques

Types of coding

This is the place where we are going to deal with the different types of coding techniques
available here. There are two forms of coding. They are
1. inline coding
2. code- behind coding

The above two types of coding are stored in a file with .aspx extension. The process
carried out in both the coding types is similar to one another.
Is there an Inline Code Justification?

Sure there is. There are certain situations where a purely inline - code
implementation is justified. However, in many of these cases, upon inspection, it can
be determined that the developers who claim they "had to do it this way" could have,
with a little study, implemented it with the codebehind paradigm after all, had they
taken the time to solve the minor complexities that seemed to at first compel them
to use an inline-only code implementation. The benefits to the developer group and
its potential new members in terms of ease of maintenance and developer
productivity will almost always be worth the small amount of pain that must be
endured to make it happen.

You'll probably hear lots of objections from developers who want to avoid using the
Codebehind model. My experience has been that most of these objections are born
more out of lack of knowledge and experience than from careful thought and study.
Do you agree? Disagree? Feel free to post your comments in our forums in the Article
Discussion area.

As architects, we understand that not all coding standard decisions are easy choices,
but having standards in an organization allows current and future developers to more
easily understand, maintain, debug, and extend the codebase they work on daily.
Make sure your company successfully resolves the inline vs. code-behind debate
before you get too far down the development path with ASP.NET. After all, you don't
want to wake up one morning with a new career where your vocabulary consists of
the sole phrase "Paper or plastic?".

Creating the Code Behind

The example in this article performs a simple task: it reverses the order of the characters in
any string you provide as input, so "Hello" appears as "olleH". In this example, when the user
clicks the button used to make the request, the Web page refreshs only an output control,
rather than all the controls on the page.
When working with client callback, you should begin writing your code in the code behind file.
The client callback process relies on the ICallbackEventHandler interface, which contains
a two methods, RaiseCallbackEvent() and GetCallbackResult().

Begin the process by adding the interface to the class declaration for your application. Here's a
typical example of the code you'll use:

Partial Class _Default

Inherits System.Web.UI.Page

Implements ICallbackEventHandler

Adding Implements ICallbackEventHandler to the class definition automatically creates


the two methods for you when you press Enter. The first method defines how to raise a
callback event from within the Web page. The second method outputs the result of the
callback to the client.

However, you still need some glue code to make all of this work, which is where a simple
variable declaration comes into play. Listing 1 shows the variable declaration and its definition
as part of the Page_Load() method.

Listing 1: Defining a Script to Raise a Callback Event


Public strCallbackFunctionInvoke As String

Protected Sub Page_Load(ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles Me.Load

' Define the event handling information.

strCallbackFunctionInvoke = _

Page.ClientScript.GetCallbackEventReference( _

Me, "Input", "ReverseString", _

"Context", "ShowError", False)

' Create the script.

Dim CallBackScript As String

CallBackScript = "function SendData(Input, Context)" + _

+ vbCrLf "{" + vbCrLf + _


strCallbackFunctionInvoke + ";" + _

vbCrLf + "}" + vbCrLf

Page.ClientScript.RegisterClientScriptBlock( _

Me.GetType(),"SendData", CallBackScript, True)

End Sub

The code begins by defining strCallbackFunctionInvoke, which is the placeholder that


tells ASP.NET where to place script in the Web page. You'll see this string again in the
"Defining the Web Page Code" section of the article. However, for now, all you need to know is
that it contains the information that the Web page needs to make a callback.

The code defines the content of this string in the Page_Load() method. The
GetCallbackEventReference() method returns a specially formatted string containing the
callback information. The callback information includes:

• a reference to the control that handles the callback


• the name of the Web page variable that holds the input
data
• the name of a function on the Web page that the callback
calls when the call succeeds (say that three times fast)
• a variable containing context information for the callback,
and
• the name of a function on the Web page that the callback
calls when the call fails.

You might also like