Tuesday, August 08, 2006

Oh No!

Done it all wrong :(

Friday, August 04, 2006

Master Pages - Event Ordering

Event Order when Master and Content Ordered
  1. Master page controls Init event.

  2. Content controls Init event.

  3. Master page Init event.

  4. Content page Init event.

  5. Content page Load event.

  6. Master page Load event.

  7. Content controls Load event.

  8. Content page PreRender event.

  9. Master page PreRender event.

  10. Master page controls PreRender event.

  11. Content controls PreRender event.


Master Pages child control Initialisation

All server controls contained within the master pages

Content page child controls Initialisation

All server controls contained within the content pages

Master Page initialisation

Page is initialised

Content Page Initialisation

Page is initialised

Content Page Load

The Page_Load event is followed by the Page_LoadComplete event

Master Page Load

The Page_Load event is followed by the Page_LoadComplete event

Master Page Child Controls load

The server controls on the master page are loaded onto the page

Content Page Child Controls load

The server controls on the content page are loaded onto the page


For Example

This is important. If you want to use control values from a master page on the content page you can't do this in the Page_Load event of the content page.

Use the Page_LoadComplete event of the content page which happens after the Page_Load event of the master page.

Wednesday, July 19, 2006

Forms and WIndows based Authentication

For Intranet applications I've done where I want to use NTLM I've done the following:
  1. Create a web application which uses Forms authentication.
  2. Set the loginUrl attribute of the forms element to "AutoLogon/AutoLogon.aspx" and set the .
  3. Create a virtual folder beneath the we app called AutoLogon which is itself an application.
  4. Set the AutoLogon web.config to use the settings you describe above.
  5. Add the following (simplified) code to the page AutoLogon.aspx IPrincipal ip = HttpContext.Current.User; IIdentity id = ip.Identity; FormsAuthentication.Initialize(); FormsAuthentication.SetAuthCookie(id.Name, false); HttpContext.Current.Response.Redirect("../", false); What this gives you is all combination of NTLM authentication without having to authenticate every page using NTLM as you can rely on the forms authentication scheme. You can also easily tie together a custom authorisation scheme with NT/Active Directory groups.

ScottGu's Blog : Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application

technorati tags:,

Convert char[] to string

char[] is quite convenient if you're reading file data using StreamReader, but it doesn't offer the same functionality as string does. Here's one way I've found of converting:

char[] charBuf = new char[1024]; // Example char array
string s = ""; // Example empty
string s = new string(charBuf); // Create new string passing charBuf into the constructor

C# Programming: Convert char[] to string

Blogged with Flock

Thursday, July 06, 2006

Creating Composite Custom Server Controls

I am in the process of creating a custom composite control.

Please comment on any mistakes or better ways you can think of doing things.

The Composite Control

The idea is to create a selector control. This involves some popping up a nice floating div in an appropriate place complete with appropriate textboxes and dropdowns for criteria selection and a pageable sortable list view underneath. The primary key or perhaps the row is clickable! this closes the box and selects the primary key. A cancel button is required also.

What's it Made of?

Inherit from CompositeControl

[ToolboxData("<{0}:Selector runat=server></{0}:Selector>")]
public class Selector : CompositeControl


The control is made up of a GridView, textboxes, comboboxes and some buttons. A LinkButton over the primary key perhaps.

Using the Control Execution Lifecycle I thought to put all the things that happen once when the control is initialised in OnInit.

Getting the data and putting it into the GridView. Also setting up some of the properties of the GridView.

Events

Right this is slightly hairy. I will describe what I have done but I think it is possible there are other solutions.

I noticed when I clicked a column to try and sort the grid that I got an unhandled event exception. Fair enough!

I went this way. Add a new EventHandler to the Sorting event on the GridView.


selectorGrid.Sorting += new GridViewSortEventHandler(selectorGrid_Sorting);

Visual Studio kindly allows you to press TAB to create the stubs and you end up with

void selectorGrid_Sorting(object sender, GridViewSortEventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}

Now when the column is clicked you end up in the selectorGrid_Sorting method and the "The method or operation is not implemented." exception is thrown.

The sort now has to be handled by the composite. Of course, you cannot use the Sort method on the GridView because you will end up in the Sorting Event and get an Overflow exception.

As the control is loaded every time any state must be preserved manually using the ViewState["myThingIWantToSave"] array.

private GridViewSortEventArgs SortArgs
{
get
{
if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
{
_sortValues.SortExpression = (String)ViewState["SortExpression"];
_sortValues.SortDirection = (SortDirection)ViewState["SortDirection"];
}
return _sortValues;
}
set
{
_sortValues = value;
ViewState["SortExpression"] = _sortValues.SortExpression;
ViewState["SortDirection"] = _sortValues.SortDirection;
}
}

_sortValues is private variable. It means I can have property of the right type and put defaults in. SortDirection is not Serializable either.

My event ends up looking like this:

void selectorGrid_Sorting(object sender, GridViewSortEventArgs e)
{
EnsureChildControls();

//reset to Ascending if changing expression
if (SortArgs.SortExpression != e.SortExpression)
SortArgs.SortDirection = SortDirection.Ascending;
else
//if the expression is the same just toggle the direction
_sortValues.SortDirection = SortArgs.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

_sortValues.SortExpression = e.SortExpression;
//Save to ViewState
SortArgs = _sortValues;
//Sort the data in the Grid
SortGrid();
}

Here is SortGrid

private void SortGrid()
{
DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
if (SortArgs.SortExpression.Length > 0 )
dv.Sort = SortArgs.SortExpression + " " + (SortArgs.SortDirection == SortDirection.Ascending ? "ASC": "DESC");

selectorGrid.DataSource = dv;
selectorGrid.DataBind();
}

Page Indexing works in a similar way except you don't have to worry about ViewState because the GridView does it internally.

Blogged with Flock

Wednesday, May 24, 2006

Expressions

Expressions in .NET

Basically this is a way of adding declarative content to a control or page or form.

This is handy as shown below for things like build information and so on. It also doesn't have to be text etc. Bind most properties to an expression.

And of course expressions are extensible.

Do it usefully in...

Application Settings

In addition to using expressions for connection strings, you can use expressions to reference application settings defined in a configuration file accessible to the Web site. For example, you might have frequently used strings, such as your site's copyright message, stored in the appSettings section of your Web.config file, which might look like the following:

 <appSettings>
<add key="copyright" value="(c)Copyright 2004 Northwind Traders"/>
</appSettings>

Within your ASP.NET pages, you could reference the value by using an expression similar to the following:

 <%$ AppSettings: copyright %> 

This would enable you to maintain frequently cited elements within

nb

You can also use it for connectionstrings and Resource files.

Displaying Static Content Using Expressions

If you want to use an expression as a static value on your page or control, you use an expression as part of an ASP.NET server control. A typical strategy is to add a Literal control and set its Text property to an expression. For example, to place a copyright notice at the bottom of every page you could use the following:

<p align="center">

<asp:Literal runat="server" text="<%$ AppSettings: copyright %>"/>

</p>

Thursday, March 16, 2006

Basic Ajax

AJAX is one of the hottest new technologies, in the ever growing universe of Web development.
In this article, we'll get to know how AJAX fits in a real-world situation and how you can assess its value in a project.
Hopefully, by the time you finish reading this article, you'll know what AJAX is, why, and how to use it.Nothing really new about what is happening here.
The normal routine in Web development involves requesting a file from the Web Server and receiving a page as response. So whats new with AJAX. Just that the difference, is that requests are being made from the Client side Java Script, which is embedded within HTML.
So what is AJAX ?
AJAX is acronym of Asynchronous Java Script and XML
Asynchronous:
Imagine you are the User, and you have sent some request to the Web server via Hypertext Transfer Protocol (HTTP). In regular web technology, you must wait for the web server to respond. In AJAX, while you are waiting for the response , you are free to do some processing on the same page you have requested. Since your response may not come back immediately what you can do is, set up a function which will wait for it to be send from the server, and which will be authorised to respond back.
Java Script :
As all we know, Java script used to make request to the server. Once you get the response back from the server you can use the document object model in some way to show output. That is, the submission is done successfully.
XML :
The data you receive back from the server, will often be packaged as a snippet of XML, since it can be easily processed with Java script. However, any type of text file can be used with AJAX.
In AJAX, the request to server is being done through Hypertext Transfer Protocol (HTTP). The speciality of AJAX is that, the client side script can continue with the process, even while waiting for the response from the Web server. So its called Asynchronous.
So lets keep this simple. We retrieve data from a database as XML, make the calls to a server-side script, then send data to a server-side script that has to be stored in a database, or simply load the XML file in order to populate pages of your Web site without refreshing the page. This article will help you to determine, when AJAX is a good solution for developing your users' experiences.I assume that one have the basic understanding of the Java, Java Script and XML part of the acronym . Although one can request any type of text file with AJAX, Here we focus only on XML. You're going to learn how to create the object, make a request, and customize the available response, while maintaining a good practice that provides an intuitive user experience. Ultimately it is the user experience that is going to count more than anything else.
Let's Do AJAX:
To begin with, you need to create an XML file, that we will use in our AJAX program. Keep in mind, that the file you're requesting must reside on the same server as the finished project resides.The request will be made by an HTML file, so we need to create an XML file as our next step . When the page loads with the help of 'onload' method in the body tag, the request is made to the server . The div tag of the HTML file will have an ID so that we can target it when we're ready to display the content. Once you've finished doing this this, the content of the body of your page should have :
 <body onload="makeRequest("'xml/data.xml'");">
<div id="copyhere"></div>
</body>
Request Object Creation:
In order to create the request object, the first thing is to check whether browser uses the XMLHttpRequest or the ActiveXObject. The main difference between the objects, is the browsers that use them. ActiveX object is used by Windows IE 5 and above ; XMLHttpRequest object is used by Mozilla, Netscape 7, Opera, and Safari 1.2 and above. The way in which you create the objects also makes a difference. While Opera, Mozilla, Netscape, and Safari, simply calls the object's constructor, the Windows IE requires the name of the object to be passed to the ActiveX constructor. Following example shows you how to write the code to determine which object to use and how do we create it:
if(window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
request = new ActiveXObject("MSXML2.XMLHTTP");
}
Making a Request to the Server:
Since we have created the request object, the request to the server can be done now. The reference must be created for an event handler to listen for onreadystatechange. So once the state changes, the event handler method will start its functionality. As soon as we finish writing, the request, we'll write this method. We now need to have a connection to GET or POST, a custom URL, which in this particular case is the content.xml, and have a Boolean defining whether you want the call to be asynchronous.Now the time has come to send the request. For this example, We will use null because we're using GET; when one use POST, along with this method, you need to send a query string.
request.onreadystatexchange = onResponse;
request.open("GET". url, true);
request.send(null);
Custom Loading and Error Handling Messages:
The place to focus on loading and error handling is the event handler that you created for onreadystatechange method . Now it is time we start thinking about the users and provide feedback on the status of the content with which they're interacting. In this example, we shall provide feedback to almost all the loading status codes and some basic feedback for the error handling status codes that seems to occur most frequently. While indicating the current status of the request object, the readyState property includes the values shown in the following table.
Value Description
0. Uninitialized. Without data the object is being initialized
1. Loading. The data is carried and loading to its object
2. Loaded. Finished loading its data by object.
3. Interactive.
Possible for user to interact with the object even while it's not fully loaded.4 Complete. The object now completely initialized.
Parsing the Response:
It is when you're ready to parse the response from the request object, that the real work begins. Now, we actually start working with the data that you requested. To display the raw data from the response for testing purposes alone during development, the responseText and responseXML properties can be used, if one feel so. To begin accessing nodes in the XML response, start with the request object that you created, while target the responseXML property to retrieve (you guessed it) the XML from the response.
Target the documentElement, which retrieves a reference to the root node of the XML response.
var response = request.responseXML.documentElement;
Now that you have a reference to the root node of the response, you can use getElementsByTagName() to retrieve childNodes by their node names. The following line locates a childNode with a nodeName of header:
response.getElementsByTagName('header')[0].firstChild.data;
Using firstChild.data allows you to access the text within the element:
response.getElementsByTagName('header')[0].firstChild.data;
Here's a complete example of how to write the code:
var response = request.responseXML.documentElement;
var header = response.getElementsByTagName('header')[0].firstChild.data;
document.getElementById('copy').innerHTML = header;
Assessing the requirements:
Now that you know the basics of how to use AJAX, to decide on whether to use it on a project will be certainly the major step. The most important thing to keep in mind while developing application using AJAX is the unavailability of the Back button when you're not refreshing the page. It may be beneficial, if you focus on using AJAX in smaller sections of your project that could benefit from using this type of interaction. For instance, you could build a form that queries a script every time a user enters an input field, or even types a letter in order to provide real-time validation. You could build a drag-and-drop page that sends data to a script on the release of an item and saves the state of the page to a database. The reasons for using AJAX definitely exist, and are beneficial not only to the development experience but to users; it all depends on the situation and execution.There are also ways to work around the issues with the Back button, such as with Google and Gmail, which now provides an undo for steps that you make without refreshing the page. Many more creative examples are sure to surface, which will benefit users by providing developers with ways to create unique, real-time experiences.ConclusionEven though AJAX may allow developers to build new,advanced and improved ways of communicating with a Web page, as an AJAXIAN one need to remember that our product is not only about the technologies we use to implement; Without the consideration of the end users, any project we build would have no purpose. If one have that principle in mind, we can assess what technologies to use and when is it appropriate to use , in order to create an efficient,improved application that's beneficial to all who use it.

AJAXing the Web :: BobCares :: Outsourced Web Hosting Support

technorati tags:

Friday, January 27, 2006

What is .NET

ASP.NET is a request processing engine. It takes an incoming request and passes it through its internal pipeline to an end point, where you, as a developer, can attach code to process that request. This engine is actually completely separated from HTTP or the Web server. In fact, the HTTP runtime is a component that you can host in your own applications outside of IIS or any server side application altogether. For example, you can host the ASP.NET runtime in a Windows form.


A Low-level Look at ASP.NET Architecture


dot NET blog

Welcome