A few months back, I was brought on to a ASP.NET project where the developers were trying to provide a portal type user experience avoiding many page refreshes. Their solution was to build one page with a ton of divs, which would show or hide based on the user’s selection in the left hand navigation. Server requests for data to be displayed would be preformed via a jQuery AJAX request. As you can imagine, the page got to be very unmanageable.

My thought was to break out the individual sections the user would navigate into separate user controls. To avoid a massive page load, these user controls would be loaded in as needed via a jQuery AJAX request already populated from their code behind file.

The first step in doing this is to create an ASHX handler to load the user control. Since ASCX controls are not made to render on their own outside of a page, this handler would have to render the control within a page and then return the html.

Skelton Post 1

Thus, the html within the page just consists of the user control requested with the parameter “control” is returned. ┬áThe next step is to make the jQuery AJAX request to call UserControlHandler.

Skelton Post 2

As you can see, the user control requested is TargetUserControl.ascx. You can also add any data the user control might need in the ajax data element, then just pass it to LoadUserControl after it has been stripped out of context.Request.

Skelton Post 3

Skelton Post 4

The next step was handling submission of form elements in of the user controls. If you take a look at the HTML code rendered in the page at this point, you’ll see that the user controls are wrapped in an empty form tag.

Skelton Post 5

Since this page is supposed to operate like a portal with no page refreshes, we’ll also submit the form contents with ajax serializing the contents of the form tag.

Skelton Post 6

Then within PageName.aspx you define a webmethod to handle the saving of the data.

Skelton Post 7

The DataParser object is a custom string parser I defined for the project. The method that’s important here is ParseSubmitString which basically splits the serialized string on ‘&’ so that you have a result of key=value strings which can then be parsed.

Skelton Post 8

Shawn Skelton (1 Posts)

Shawn is a Technical Consultant at Information Concepts with a Bachelors Degree in Computer Science from Eastern Washington University.


Shawn Skelton (1 Posts)

Shawn is a Technical Consultant at Information Concepts with a Bachelors Degree in Computer Science from Eastern Washington University.