Storing a List<> of Objects in ViewState

Basically what I initially wanted to do was create a GridView in ASP.NET and add items to the Items collection programmatically. This isn’t possible though, GridView doesn’t have an items collection because it’s a DataBoundControl and not a ListControl like f.e. a ListBox, RadioButtonList, CheckBoxLIst etc.

I couldn’t just go and create a temp database where I would insert the data and bind this table to the GridView.

The scenario what I have is the following. The user sees a page, enters some data in a couple textboxes and has the ability to add records that are linked to the the info in the textboxes. I want to process everything when everything is filled in. I know it’s not common but anyway..I couldn’t come up with an idea at first so I did some thinking. I had to have something that would store these records, bind them to the GridView and stay there after PostBacks to the server (Adding of new record f.e.) occur. Ok so State Management popped in my mind. I wanted to make use of the ViewState, you can store info there within the bounds of a single page.

A simple example of ViewState is the following:

//Set Value in ViewState ViewState["Blog"] = "MsHelp"; //Get Value from ViewState if (ViewState["Blog"] != null) { Response.Write(string.Format("My blog is called, {0}", ViewState["Blog"])); }

If you wouldn’t check the ViewState for a null value and it would for some reason be null, a NullReferencePointer Exception would be thrown.

Now, except for storing strings, ints or any other primitive type, you can also store objects in the ViewState. This is done as follows:

//Customer Class [Serializable] public class Customer { public string CustName { get; set; } public string CustAddress {get;set; } } //ASP.NET Code Behind file //Create Customer object Customer cust = new Customer(); cust.CustName = "Sven"; cust.CustAddress = "Vlinderlaan 99"; //Set Customer object in ViewState ViewState["CurrentCustomer"] = cust; //Get Value from ViewState if (ViewState["CurrentCustomer"] != null) { //Create a new Customer object Customer OldCustomer; //Cast ViewState Object to Customer Object OldCustomer = (Customer)ViewState["CurrentCustomer"]; Response.Write(string.Format("Hello, {0}. You seem to be living on the {1}",OldCustomer.CustName,OldCustomer.CustAddress)); }

 

Mind the [Serializable] attribute in front of the Customer Class. This is needed the object has to be converted to a stream of bytes so it can be added to a hidden field on the page, this is done using Serialization. If this attribute wouldn’t be supplied you would end up with a System.Runtime.Serialization.SerializationException.

We’re almost there, but the problem here is, I don’t want to store a single object in ViewState. I want to store several objects and bind these to a GridView so the user has a nice list with columns and can see what kind of records he/she added. Ok let’s at this final step. Like I said, we’re almost there.

Ok so let’s first take a look at the asp.net page. I need a GridView and some textboxes to add a customer with a name and address. I added 2 boundFields to the GridView with CustName and CustAddress as DataField.

<div> <table> <tr> <td> <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" EmptyDataText="No customers added!"> <Columns> <asp:BoundField DataField="CustName" HeaderText="Name" /> <asp:BoundField DataField="CustAddress" HeaderText="Address" /> </Columns> </asp:GridView> </td> <td style="vertical-align: top;"> <table> <tr> <td> Name: </td> <td> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Address: </td> <td> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="btnAdd" runat="server" Text="Add Customer" OnClick="btnAdd_Click" /> </td> </tr> </table> </td> </tr> </table> </div>

Now let’s look at the code behind of this page.

protected void Page_Load(object sender, EventArgs e) { BindCustomers(); } private void BindCustomers() { if (ViewState["Customers"] != null) { gvCustomers.DataSource = (List<Customer>)ViewState["Customers"]; gvCustomers.DataBind(); } else { List<Customer> customers = new List<Customer>(); ViewState["Customers"] = customers; } } protected void btnAdd_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtAddress.Text)) { Customer cust = new Customer(txtName.Text.Trim(), txtAddress.Text.Trim()); ((List<Customer>)ViewState["Customers"]).Add(cust); BindCustomers(); txtName.Text = string.Empty; txtAddress.Text = string.Empty; } }

As you can see in the Page_Load event I call the method BindCustomers. This checks if the ViewState is not null, if it isn’t bind it to the GridView. You see that I first cast the ViewState to List<Customer>. This indicates that it’s a list of Customer Objects. If the ViewState is null, let’s create a new list and add it to the ViewState.

In the Button Click event I check if the Textboxes aren’t null of empty. If they aren’t I create a new Customer Object (I added a constructor to the Customer Class). Then I cast my ViewState to the List<Customer>. This is done between brackets. Now the intellisense has all the List operations and knows the CLR it’s a List. Then I add the Customer object to the list using the Add() method. Now another Customer object is added to the ViewState. I bind again and clear the Textboxes. For people that haven’t worked with List<>, don’t forget to reference System.Collections.Generic; otherwise List<> will not be recognised.

image

Some people may think this is a very uncommon scenario. I just was in this situation and I wanted to make it work, this is how I did it and it worked.

Linux Mono on IPhone

As you can see it is possible to write .NET applications for the IPhone. It’s not really the .NET framework as we know it but the Linux variant Mono. As you can read on the site, Mono provides the necessary software to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix.

image

Everything you wanted to know about MOSS Enterprise Search

Microsoft published a deep technical training about Microsoft MOSS Enterprise search. It talks about everything there is to know about MOSS Enterprise Search in 14 streamed video’s. Subjects that will be discussed are:

- Introduction

- Enterprise Search Overview

- Sharepoint search Walkthrough

- Search Architecture and Deployment

- Grawl and Query Processes

- Relevance Ranking

- Customizing the End-User Experience

- Developing Search Solutions

- Business Data Catalog Search

- Extensibility and Integration for Search

- Search Administration

- Security for Search

- Performance, Scalability and Capacity Planning

- Search Operations

As you can see, thats a lot. I think this is a great training for developers and administrators. For more info see this site

Microsoft Videos

Microsoft released a new video centric site. Ok I hear you thinking, but we already have SoapBox a.k.a. MSN Video ?? Yes that’s what I thought too when I first heard about this site. But the BIG difference is that this site focusses mainly on Microsoft related things. So if you want to stay up to date, see video’s about new Microsoft technology, then this is the site for you, check it out. Microsoft Videos Beta

Office Open XML (OOXML) gets the alias IS 29500

There was a lot of fuss over the last 15 months between many national bodies that would recognize OOXML as an ISO standard. OOXML was recognised as a standard by ECMA back in 2006 and they submitted it to ISO/IEX JTC1. They then had to decide if OOXML would become a standard or not. This didn’t go that smooth, but hey..majority rules and OOXML got enough votes to become a standard. More info can be found on the ISO site.

I think this is a good thing, OOXML will be something that will be adopted very soon in business like and other application to create on the fly Office 2007 files.

17 April: Microsoft Surface launch in US

I’ve done a couple of posts about Surface, so you should know what I’m talking about. If you don’t, use the search function on my blog :)

So finally surface will be shown to the public, this will be in several AT&T stores in America. Too bad not here in Belgium, but this will probably come in the future. It all depends on the success of the product in these stores, if all goes well it will spread on.

More information about the official press release can be found here. And if you want to know more about the launch in the AT&T stores, check out this link.

 

Open source Silverlight 2.0 charting library

Visifire released an open source library which you can use you create stunning 2D and 3D looking charts. With very nice effects, see the video below. I must say Silverlight is being adopted by many big companies, I just knew this would be a success.

You also see a lot of enterprise oriented application which is a good thing. This means they see the capabilities of Silverlight, they know that they great things with it.

Here is the video I embedded from the Visifire site, I just love those effects. I’ll have to try this out someday when I have a bit more time to experiment.


Visifire Gallery from visifire team on Vimeo.

By the way the library can be used with: ASP, ASP.Net, PHP, JSP, ColdFusion, Ruby on Rails or just simple HTML.

Check out the Gallery for actual live demo’s!

Enjoy

« Vorige pagina