Data Binding

29 05 2009

Back in PHP, I wrote loops. Loops that would take data from a single select query (no, I didnt write multiple select statements… you’re funny 🙂 )… and then put that data out in columns etc

ASP.NET – say hello to DataBinding. Apparently its been around for quite some time, but it used to be slow. And now its not. So, there is a DataList object. Kinda simple. But use GridView. Its newer and better.

Essentially, you write a stored procedure giving you the output you want after accepting some input variables. Click on the GridView. That cute little “>”  gets a click and you add your data source. Define your connection string in web.config (so that it can be changed later). Enjoy the GUI. I am storing the login name in the Session info. Will find out soon enough if this is the right thing to do. It even lets you do a test query by allowing you to specify the input variables… CUTE too CUTE 🙂

Since the stored procedure gives you certain columns, that is cool and data-bound. Now you may want to change lots of stuff.

1. You want to have your own column where you add data on the fly. So add a new column. A Template column if the others are not nice. Add whatever you want inside <ItemTemplate> tags inside this

2. For GirdView, there is a rowbound event handler. Once a row is populated with bounded sql data, this function gets called… and you get to meddle with that row (change the values fom sql.. use those values to generate data for your new columns… do whatever)

3. Rowcommand is what you use to monitor clicks. The event argument e carries data. Make sure to add proper commandnames and commandarguments if they are buttons. Do this in the rowbound function.

Another nice thing is that it auto-sorts when you click on the column header names. It basically calls Rowcommand. Now if you redefine your rowcommand, just dont do anything if you detect a sort command (Commandname is “sort” ) I guess it calls another “sorting” function that does the trick after that.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: