Archive for category ASP.NET

ASP.NET Controls, How I Hate Them

MSDN Error
Image by ttrentham via Flickr

I’ve always, for some reason, felt innately that PHP allowed me more control over my code than ASP.NET. My brain kept saying “but .NET is more organized! It compiles! It’s faster! It’s easier to write,” but my mind kept saying “PHP lets me do what I want how I want it… screw .NET!”

What I finally figured out was that I love C#, I even like the .NET framework, but I hate is, in fact, ASP.NET.

Every time I see an example of simple, elegant code, the most complex control on the page is a label or a panel. While the intentions behind FormView may be good, writing my own forms and hooking them up saves hundreds of lines (literally- I just refactored almost 800 lines of code into 150 by removing a formview) as well as reduces complexity and maintenance (now I no longer have to maintain view and edit and whatever other modes FormView has.) ASP.NET perhaps made sense in a day before OO principles and ORMs came into play; the controls were written for the same kind of people that use the drag-and-drop design mode. Easy to slap down haphazardly, not so easy to maintain.

We replaced every ASP.NET Ajax control we used anywhere (after I evangelized it, to my chagrin) with jQuery after about 6 months of use; while the controls did what we needed on the surface, underneath there was always some caveat, like the linked DropDowns needed web services, or the datepicker control had missing options… there was always something somewhere that I needed a bit of flexibility on that just wasn’t there, or was buggy. It seemed very odd for it to be out of beta in such a state. So, I ended up starting my own control library using jQuery, and now it’s easily extensible, easy to modify from the client, and I can control the markup.

Oh, and the markup… don’t get me started on the markup. Tables for everything. I can’t rearrange the otherwise useful Wizard control because it’s so static in its display.

So, I guess the point I’m trying to make is, that the longer I use .NET, the less and less I use the complex controls and the more I roll my own. Because it’s easier.
Kind of ironic.

Reblog this post [with Zemanta]

Tags: , , , , , , , ,

Tracking AJAX in ASP.NET with Google Analytics

Google analytics for recruitment
Image by carveconsulting via Flickr

(skip down a little further if you don’t need an introduction to Google Analytics.)

I love Google Analytics. In fact, I’m a big fan of Google as a whole, and I do the majority of my work using Google Docs, Calendar, and Gmail (online collaboration! oh, and free) and Google Analytics is a beautiful tool for businesses, whether their website‘s primary focus is online sales or just a small info-about-my-business site. If you don’t have some kind of tracking, you’re missing out on very important information; GA is a good place to start. Anybody can put GA into their website, and everybody should put something in.

The way GA works is by dropping a snippet of Javascript into your page; this javascript runs a series of tests against the visitor’s browser, checking screen resolution, flash capabilities, seeing if the user is unique, watching the user’s path through the website, checking the user’s location, and much, much more (all collected anonymously). This is all put into an interface where you can see the data collected and organized. However, AJAX applications don’t function as normal websites- you don’t get a new page hit every time you fire off an UpdatePanel, because it’s not a full page refresh. So, we need to do a little trickery to get things to work the way we want them to.

(you can start reading again if you skipped earlier.)

If you have the Google Analytics in the host page (whether the aspx page, or more likely, the master page), then you have to register a client script block that calls the trackpageview method. If you use jQuery, it’ll look something like:

ScriptManager.RegisterClientScriptBlock(UpdatePanelID, typeof(UpdatePanel), "uniqueIdentifierString", "$(document).ready(function(){ pageTracker._trackPageview('/pagename'); });", true);

If you’re not using jQuery, you’ll have to do a little more work to attach to the window’s onload event, but it’s pretty similar.
What you’re doing is registering a script block to execute when the UpdatePanel updates (because it won’t execute JS returned in the text), and using the pageTracker object (that the GA code you copied when you first set up GA on your site created) to force a pageview for a page you define. For my applications, I generally use something like “/dataentry/guestbook/edit” or “/dataentry/guestbook/delete” so that I can easily track guestbook views, as well as edits / deletes. It’s both a way to track controls you load via AJAX, and a cheap shot at logging (not perfect data, though, so you’re still best off doing all of your own logging on events, of course.)

The official Google help doc on the subject is also here: http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55519

Reblog this post [with Zemanta]

Tags: , , , , ,

Snakes on a Compiler

I went to the Day of .NET, an all-day “geekfest”, a sea of polos and khakis; and there, I made the final decision of what language I would learn next: Python. Well, specifically, IronPython. I guess a normal Python isn’t scary enough, so we’ll make it out of metal. (Presenter’s website; presenter’s slides; presenter’s twitter)

CPython
Image via Wikipedia

The really, really cool thing about IronPython is the ease in which I can pull in other CLR languages; namely, C#. This will supposedly get better when Visual Studio 2010 comes out and the DLR is revamped and IronPython support grows. What this will allow me to do is build all of my base classes, and my data layer, which I’m more comfortable with, and use Python for all the fun stuff- services, and display. I’ve been considering writing my next game in Python anyway, influenced by Eve Online‘s incredible sucess using the language, so this fits in perfectly with my prior knowledge.

I’m pretty excited. I’ve never heard a negative thing about Python; everyone I’ve ever heard who uses it loves it.

Reblog this post [with Zemanta]

Tags: ,

Thinking Outside the Cube

Learning outside of the workplace is, in my opinion, of the utmost importance. Whether it’s reading a book, going to a convention, visiting community websites, keeping a blog, doing freelance work, or just messing around with code for the heck of it- we must sharpen our saw.

North view of cubicle
Image via Wikipedia

I believe the key point of this is that it must not feel like work. You shouldn’t feel pressured, or dread doing something; you should do it because you want to do it. The great thing about  our profession is that we can get away and do several things, all subject to our whims; if we spent all day in a chair at a desk, and we want to get up, go to a meeting like the Day of .NET (Central Ohio this Saturday!) If you like to read, do that instead; it’s good to keep a bookshelf full of interesting books. Go into some kind of technical cross-training, such as learning a new language. Do design if you’re a devleoper, or develop if you’re a designer. Just do something.

In this way, we can continue to work on our skills, learn new things and keep from being bored. And, at least in my experience, you can bring new skills and solutions to problems you’re already facing at work.

Reblog this post [with Zemanta]

Tags: , , ,

User Controls Rock: Legos and Fake Ajax Master Pages

Let me start with a simple statement: user controls are absolutely fantastic.

If you haven’t delved into the realm of the .ascx, then let me briefly explain the two big benefits that have been relevant to me recently:

  • Totally reusable code
  • Emulate master pages; only with this, you can use the Ajax control toolkit to do it

On the first point, the reusable code point, this is the biggest. It’s the entire reason behind user controls. It lets you chunk out code that you use all over, and use it in several places; it’s a fundamental of object oriented development. Let’s do an example.

Say you have a piece of code that goes to the database and retrieves data on a member. That data is then put into a styled gridview, for the administration side of a website. But, you also have an area in a members section where a user can edit their own information (more like a formview); and, on the normal web display, casual users can browse through usernames and user roles in a paged gridview.

Don’t write the same code thrice! (or even twice!) Rather, make a user control to do all the work for you, and place it in each of your pages. This is how I’d do it:

For the sake of this article, I’m assuming you’re using a list of “Member” objects. Use whatever.

-Make a public class-level property in your control called “ReadOnly”, a bool.
-Make a public class-level method in your control called “Intialize”, which takes an IList of Member objects
-If the IList has one object, add a FormView to your control, and databind it. Otherwise, go the Gridview route (doing this all from the codebehind would keep your code cleaner and keep you from having an extra control rendered, although you could do this in the .ascx if you really wanted to; just set one to visible=”false” to the one you’re not using)
-Create all of the appropriate methods for your control for delete, update, insert, etc. and set up your formview or gridview accordingly (of course, only update for our member’s area FormView!)
-If ReadOnly is true, don’t add edit, insert, or delete buttons to your formview/gridview. (Easy enough; could set visible=”false” on the fields, or just not add them at all)

And, then, back on your three main pages, register and use your control.  Set the ReadOnly property (if it’s a bool, you should even get IntelliSense for true/false), and then on Page_Load, call Intialize on the control, passing through the IList of Members that you pulled down.

The reason we’re calling the data on the page, rather than the control, is so that the control can remain “dumb” and doesn’t need to know whether it’s a member, admin, or web display control. The less logic in the control, the better; just spit out the lowest common denominator.

Ok. Now that we’ve hit the reusable control side, let’s hit the fake-an-ajax-master-page part.

As you may or may not know, even if you wrap the ContentTemplate in a MasterPage, it still reloads every page change. This is because the MasterPage gets loaded after the rest of the page, and is treated like a control. So, if you change pages.. it loads the page, and then says afterwards, ‘oh, yeah, that was Ajax. Oh well.’

The cool thing you can do, is make one default page, and inside of that have user controls in place of your pages, something like this:

-Make your default.aspx page
-Create user controls; however, rather than logic in Page_Load of the control, put it all in a public class-level method you call Initialize.You’ll see why soon.
-Put all of your controls into your default.aspx page, with visible=”false”.
-On the onclick of your navigation buttons, run the Initialize on the control, and set it’s visible property to true, and all the others to false.. I might alternately suggest using Command and a single method rather than Onclick and seperate methods for each button, so you can pass through a CommandName, which you can then use in a switch statement and know what button you hit.

Ok. Now that we have our fancy Ajaxy page, you may be wondering: why not just visiblity? Why this Initialize thing?

This is because ASP.NET will fully render the controls, visible or not. Which means that if you have 10 pages, it will load those 10 pages and display one; which is a big performance hit on the server, especially if you’re loading database data into each of those 10. Having to explicitly call Initialize avoids this. It also allows you to pass through a common parameter to each of the controls (say, a title string that displays at the top of each control) .

Controls can certainly do much more than this; but these are the biggies for me. Have any .ascx uses of your own?

Tags: , , , , ,