Thursday, June 28

ASP.Net FormView Control.

&t
I was posed with the problem of a showing a list of data, displaying the details of a particular enry in the list and letting the user edit/update the entry, all on one page. I have seen such an scenario implemented with gridview with an "Edit" button as a column on the gridview and displaying the details on a modal popup. the modal popup would be either a "showModalDialog" based window or an Telerik RadWindow.

I wanted to specifically avoid having popups due to cross browser issues (did not have Telerik suite available for this project). To implement all of this functionality on a page I decided to user the Master-Detail mode with either the GridView-DetailsView or the GridView-FormView.

I srated off with a DetailsView by using TemplateFields for editable fields. soon, I realized the I had no control over the layout of the DetailsView and since I wanted to keep evething in the 1024X768 boundry, I decided on using the FormView.

FormView is very much like the DetailsView, it displays data item by item with paging support. What it offers you over the DetailsView is the ability to control the layout of the elements of the Detail and it also aleviates the need to implement an TemplateField for eahc and every field that needs to be edited. 

It basically has an ItemTemplate & an EditTemplate, the contents/layouts of which are totally in the developers control.
  319         asp:FormView ID="fvExceptionDetails" runat="server"/asp:
  320         AllowPaging="true" DataKeyNames="Oid" 
  321         OnModeChanging="fvExceptionDetails_ModeChanging" 
  322         OnPageIndexChanging="fvExceptionDetails_PageIndexChanging" 
  323         OnItemUpdating="fvExceptionDetails_ItemUpdating">
  324             ItemTemplate
  325             /ItemTemplate
  326             EditTemplate
  327             /EditTemplate
  328         FormView>
Since I am well versed with the event model of the GridView, I felt FormView would be similar to use and implement. But it is a bit different than the GridView and here are the events that need to handled in order for the FormView to work.

Each entry in the list displayed in the GridView was associated with zero or more detail entries that needed too be displayed in the FormView. Hence use of paging was obvious choice and had to have an handler in place for the PageIndexChanging event. 

  261     protected void fvExceptionDetails_PageIndexChanging(object sender, 
  262         FormViewPageEventArgs e)
  263     {
  264         fvExceptionDetails.PageIndex = e.NewPageIndex;
  265         DataBindExceptionDetails();
  266     }

The ItemTemplate had a LinkButton that had an CommandName="Edit" that when pressed should transform the FormView in the "Edit" Mode. so had to handle the ModeChanging event. Since I had did not want the user to move to anoher while editing, I disabled the Paging when in this mode. Also, I found that it was critical to DataBind() again in this even handler.

One thing to note would be that, in order to be able to access any of the elements inside the EditItemTemplate, the FormView has to be in that mode before you try and find the control using .FindControl("txtComment"). Hence the first statement in the ModeChanging event handler is the the .ChangeMode() method call.
  267     /// 
  268     /// event invoked only when mode changed through UI.
  269     /// 
  270     /// 
  271     /// 
  272     protected void fvExceptionDetails_ModeChanging(object sender, 
  273         FormViewModeEventArgs e)
  274     {
  275         fvExceptionDetails.ChangeMode(e.NewMode);
  276         if (e.NewMode == FormViewMode.Edit)
  277         {
  278             fvExceptionDetails.AllowPaging = false;
  279         }
  280         else
  281         {
  282             fvExceptionDetails.AllowPaging = true;
  283         }
  284         if (e.NewMode != FormViewMode.Insert)
  285         {
  286             DataBindExceptionDetails();
  287         }
  288         SetJSonclickAttribute();
  289     }

The EditTemplate contained 2 buttons at the end, namely the "Update" and "Cancel". In order to implement the "Update" functionality I had to have implement the ItemUpdating event handler. 

  322     protected void fvExceptionDetails_ItemUpdating(object sender, 
  323         FormViewUpdateEventArgs e)
  324     {
  325         //perform update logic here.
  326         fvExceptionDetails.ChangeMode(FormViewMode.ReadOnly);
  327         fvExceptionDetails.AllowPaging = true;
  328;/span>         DataBindExceptionDetails();
  329     }  

I hope this implementation detail helps. One thing to note would be that I was not using an ObjectDataSource/SqlDataSource to populate the Detailsview.

Sunday, June 24

ASP.Net Page Cycle

Understanding the ASP.Net page cycle is important and helpful in cases where you need to make changes to a control/ add a conrol/ change the master page at runtime, etc..

Here is the sequence of events and a brief description of the critical ones. I haven't had the chance to use of all of them as yet.

Order top to bottom

PreInit (method: Page_PreInit)

Themes and master pages are applied between PreInit and Init, hence PreInit is the last place to programtically modify the selected theme/masterpage. When implmenting "Nested Master pages" while maintaining the ability to see pages in design mode in VS 2005, one needs to set the the change the page's current master page ( the base master page) to the nested master page using this event.

Init
This event is called before any state is restored from the
client response. It is used to prepare the Page for processing the request. Dynamic controls can be added in this event.
InitComplete
Child controls are created between this event and the "InitComplete" event if it is a postback. Also, Control/View State is loaded if it is a postback.
PreLoad

Load
Most commonly used event. It is used to initialize control values on a GET request (!IsPostBack). It is called after
the control/view state has been restored in the Init event on a POST request (IsPostBack) before control passes to the specific event that caused the postback.
LoadComplete
To be used to move code from the PreRender event,
the place to make last minute changes to controls.
PreRender
Used to make any post-event modifications to the controls (Last place to make a change before they get rendered). Recently, I created an editable gridview consiting of 2 columns, one a "State" dropdown and the other "Type" dropdown whose content needed to be changed based on the state selected. I had a Telerik RadAjaxPanel around the Grid and wanted to change the content of the "type" dropdown based on the selection in the "State" dropdown. I had specifically used the PreRender event to tie the "State" dropdown to the "Type" dropdown for every row in the Grid.
PreRenderComplete

Render

UnLoad

Tuesday, June 12

Safari Beta 3 for PC released

I am excited, Apple has released Safari Beta 3 for the Windows. Though it crashed on my machine and I still haven't been able to see it run on my machine, I did see it running on my colleague's win xp box.

A substantial amount of education related community use the mac's and we had a lot of requests to make the sites "Safari" compatible.

This is a big player entering the pc browser market and should make things interesting.

Get it at Safari Beta 3 for PC

Saturday, June 9

Asp.net Release mode Request timeout

On one of the projects, the administrator triggered of an event that created a flat file based on the contents of a database. This task needed around 10-20 minutes to complete. The task completed fine when in debug mode but never in release mode.

Upon talking to an Architect in the company it dawned upon me that there ASP.NET has something called "Request Timeout" (seperate than Session timeout) and this varies between the debug and the release mode. For the debug mode, the duration is a year while for release mode the request timeout is 90 seconds. Hence the abrupt request termination.

He pointed me to this:
Code Better: Peter Gekko: Timeout of an asp.net page

In short the code looks like this:
  279     protected override void OnInit(EventArgs e)
  280     {
  281         timeOut = Server.ScriptTimeout;
  282         // Give it 5 hour = (3600 * 5) = 18000 seconds
  283         Server.ScriptTimeout = 18000;
  284     }

This task can be completed using an ASP.NET Async page or by spawning a separate thread/process. I will make a post on that experience. Till then, this is workable.

Friday, June 8

Javascript gotcha

I recently ran into an issue with JavaScript where the script tag was ended with

Please overlook the missing "<" at the start of the script tags, since blogger understandingly has issues with script tags.


script src="../jscalender-1.0/calender.js" />
script src="../jscalender-1.0/calender/look.js" />
script src="../jscalender-1.0/calender/feel.js" />
script language="javascript" type="text/javascript">
function showCalender()
{
}
/script>


In spite of the rendered page containing the above script blocks, the showCalender function was not found and I kept getting a runtime javascript error: Object not found.

IE provided no clues about what is happening, finally when I looked at the source in Firefox with the color scheme, it dawned upon me that only the first script tag is being recognized while the rest are being ignored.

When I changed them to:
script src="../jscalender-1.0/calender.js"> /script>
script src="../jscalender-1.0/calender/look.js" > /script>
script src="../jscalender-1.0/calender/feel.js" > /script>
script language="javascript" type="text/javascript">
function showCalender()
{
}
/script>


So the moral is, close the JavaScript tags with /script> even though the /> might work in certain cases, it baffles you in cases like this.

Saturday, June 2

Books and Tree Surgeon

I am almost about to finish Joel on software. It is a fun book for developer and actually puts a smile on your face. I really liked his style of writing and presenting this experiences.

He says on recruiting, he only looks for 2 qualities in "Smart" and "gets things done".

I also like his "Joel Test" for evaluating employers. Quoting him:

1.Do you use source control?
2.Can you make a built in one step?
3.Do you make daily builds?
4.Do you have a bug database?
5.Do you fix bugs vefore writing new code?
6.Do you have an up-to-date schedule?
7.Do you have a spec?
8.Do programmers have a quite working conditions?
9.Do you use the best tools money can buy?
10.Do you have testers?
11.Do new candidates write code during their
interview?
12.Do you do hallway usability testing?


Also interesting is his idea about "hallway usability testing", which I actually implemented on a recent project. Its amazing how much input you get from fellow developers, their criticism, ideas to make the ap better. Hallway testing will stay with me forever.

I recently bought Code Complete, Jeff Atwood swears by this book. I want to read this book, its a big one. He considers it as the bible for all software developers.

I went to Barnes and nobles to read a book called Head First Design Patterns, I really liked the book and concepts were explained so beautifully. I just read about the "Strategy Pattern" and loved the way they presented the materials. I liked the line "Just because you know every OO concept doesnt mean you know how to use they".
some of the concepts I can still remember are
Encapsulate everything that changes
Use Composition over inheritance
Code to Interfaces and not implementations.

I came accross an amazing tool called "Tree Surgeon" which creates a development tree for you. just give it a project name and "voila" it creates a development tree for your project. One of the criticism asp.net received from David Heinemeier Hansson was that asp.net gives you a clean slate when you start with a new project. One has to be knowledgeble enough to setup the testing framework, business layer, build project. If you are new to .Net and not fortunate enough to have a good mentor, you would probably end up with a bloated project. Tree Surgeon does all that for you.

I am going to try and use it in some home project, probably a project to do an existing project using the MVP pattern. I will report my findings.

It will also introduce me to NAnt build system. I have only been using VS 2005 for building my projects uptill now.

Friday, June 1

IIS User

It might be a 101 but this week it struck me that the user under which IIS runs differs in WinXP and Win 2003. I was always under the impression that IIS runs under the local aspnet account (hostname\ASPNET).

Well, it turns out that it runs under ASPNET in XP (which most developers have) but runs under "Network Service" in Win 2003 which most servers have. I was setting an old website for maintainance purposes and the IIS user needed write permission to a certain folder (for ActiveReports PDFs to be written) .

Cassini, the internal web server for VS 2005 seemed to have access to that folder fine. I then setup a virtual dir for this project on my local IIS and gave ASPNET user write access and it worked. It took me a while to realise that under Win 2003, IIS runs under "Network Service".

Anyways, here is an article about it.

How To: Use the Network Service Account to Access Resources in ASP.NET



This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]