About UsCommunityTrainingContent DevelopmentContact

Blogs
Pluralsight
Course Schedule
Scott Allen
Craig Andera
Mark Baciak
Don Box
Keith Brown
John CJ
Tim Ewald
Jon Fancey
Jon Flanders
Vijay Gajjala
Kirill Gavrylyuk
Ian Griffiths
Martin Gudgin
Jim Johnson
John Justice
Mike Henderson
Joe Hummel
Matt Milner
Ted Neward
Fritz Onion
Brian Randell
Jeffrey Schlimmer
Aaron Skonnard
Dan Sullivan
Herb Sutter
Doug Walter
Jim Wilson
Mike Woodring

My Links
Home
Contact
Login

Blog Stats
Posts - 170
Stories - 0
Comments - 1085
Trackbacks - 219

Inter alia
My book
My wiki
Speaking
Webcasts

Archives
May, 2008 (1)
Apr, 2008 (3)
Mar, 2008 (6)
Feb, 2008 (4)
Jan, 2008 (2)
Dec, 2007 (1)
Nov, 2007 (3)
Oct, 2007 (1)
Sep, 2007 (4)
Aug, 2007 (1)
Jul, 2007 (3)
Jun, 2007 (3)
May, 2007 (2)
Apr, 2007 (3)
Mar, 2007 (4)
Feb, 2007 (2)
Jan, 2007 (3)
Dec, 2006 (1)
Nov, 2006 (3)
Oct, 2006 (6)
Sep, 2006 (2)
Aug, 2006 (7)
Jul, 2006 (1)
Jun, 2006 (5)
May, 2006 (2)
Apr, 2006 (2)
Mar, 2006 (5)
Feb, 2006 (3)
Jan, 2006 (4)
Dec, 2005 (6)
Nov, 2005 (3)
Oct, 2005 (6)
Aug, 2005 (6)
Jul, 2005 (4)
Jun, 2005 (4)
May, 2005 (2)
Apr, 2005 (8)
Mar, 2005 (4)
Feb, 2005 (5)
Jan, 2005 (2)
Dec, 2004 (2)
Nov, 2004 (6)
Oct, 2004 (7)
Sep, 2004 (6)
Aug, 2004 (3)
Jul, 2004 (5)
Jun, 2004 (3)
May, 2004 (1)

Post Categories
Personal(rss)
Technical(rss)



Friday, May 16, 2008

If you've been working with UpdatePanels in ASP.NET Ajax, I'm sure you appreciate how simple they make it to add Ajax behavior to almost any ASP.NET page. You can easily isolate portions of your page to update independently using asynchronous callbacks instead of refreshing the entire page with a standard post-back. In order to keep the programming model identical to that of a synchronous post-back page, however, the UpdatePanel-initiated request will run through your server-side page logic in its entirety, which is often doing more than needed to fulfill the request to update only the contents of the UpdatePanel that actually initiated the request (this is assuming you have set UpdateMode='Conditional').

If you have every tried to optimize your page logic to perform only the steps necessary to populate the contents of the UpdatePanel that issued the request, you may have run across the alluring attribute IsInPartialRendering available on the UpdatePanel class. Unfortunately this attribute is always false until deep into the rendering code (or so I've been told, I've never actually seen it get set to true myself) even if the UpdatePanel you are querying was the one to initiate the request. However, you can identify which UpdatePanel issued the async postback by querying the ScriptManager's AsyncPostBackSourceElementID property, which tells you the ID of the control that initiated the async postback, from which you can then infer the corresponding UpdatePanel. For example, if you had the following UpdatePanel defined:

<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional">
 
<ContentTemplate>
    <asp:Label ID="Label1" runat="server" Text="Panel Created" />
    <asp:Button ID="Button1" runat="server" Text="Refresh Panel 1"/>
  <ContentTemplate>
<
asp:UpdatePanel>

you would know that it issued the async postback if AsyncPostBackSourceElementID of the ScriptManager was set to Button1. You could then implement your Page Load handler to set the text of the label in the panel only if it was a) not an async postback or b) it was an async postback initiated by this particular UpdatePanel:

protected void Page_Load(object sender, EventArgs e)
{
 
if (!ScriptManager1.IsInAsyncPostBack ||
     
ScriptManager1.AsyncPostBackSourceElementID == "Button1")
 
{
   
// Refresh panel 1 data
   
Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
 
}
}

In this case we're not really saving much in the way of server-side processing, but if we had to query a database or invoke a Web service to populate the label, the savings could add up quickly. Note that if you end up using this technique, you must anticipate all controls that might initiate an async postback from within an UpdatePanel (or those marked as async triggers outside the panel) and test for all of those identifiers.

Now, on to the main point of this post – if you're using declarative data sources (like the SqlDataSource or ObjectDataSource) to populate data-bound controls within an UpdatePanel, you can use this same technique to pre-empt the data source from retrieving data if the async postback was not initiated by one of the controls in the UpdatePanel being updated. The technique I use is to add a handler for the Selecting event of the data source, and if you determine that this data source should not bother retrieving data, set the Cancel property of the SqlDataSourceSelectingEventArgs parameter passed into the handler.

<asp:SqlDataSource ID="_authorsDataSource" runat="server"
                   OnSelecting
="_authorsDataSource_Selecting">

// in code behind
protected void _authorsDataSource_Selecting(object sender,
                                           
SqlDataSourceSelectingEventArgs e)
{
 
if (!_getAuthorData) // _getAuthorData populated with t/f based on update panel
   
e.Cancel = true;
}

So there you go, with a little effort you can not only minimize the data flowing from the server to the client with UpdatePanels, but also the amount of work done on the server.

posted @ 10:35 AM | Feedback (2)

Wednesday, April 16, 2008

I've been working on a project recently where I had the need to randomly shuffle all of the rows in a DataTable. I wanted to do it with the DataTable itself instead of in the act of populating the DataTable for a couple of reasons: 1) I wanted to keep the DataTable in memory and shuffle it in place multiple times without going back to the source, and 2) I had multiple sources where data was coming from (SQL and XML) so I preferred to keep the randomization logic in one place. I also didn't want to copy all of the data (even though it was not a large amount) each time I shuffled, so I decided to use a DataView to display the data shuffled each time I needed it.

Here's the utility function I came up with - each time you call RandomizeDataTable it will return a newly shuffled DataView of all the data passed in through the DataTable. Note that because I reuse the added column "rndSortId" each time, any DataViews retrieved from previous calls to the method will have the new shuffle order. You could change this behavior by adding a new column each time with its own unique sort sequence.

As always, comments/improvements welcome – enjoy!

public static class DataSetUtilities

{

static Random _rand = new Random();

 

public static DataView RandomizeDataTable(DataTable dt)

{

// Create array of indices and populate with ordinal values

int[] indices = new int[dt.Rows.Count];

for (int i = 0; i < indices.Length; i++)

indices[i] = i;

 

// Knuth-Fisher-Yates shuffle indices randomly

for (int i = indices.Length - 1; i > 0; i--)

{

int n = _rand.Next(i + 1);

int tmp = indices[i];

indices[i] = indices[n];

indices[n] = tmp;

}

 

// Add new column to data table (if it's not there already)

// to store shuffle index

if (dt.Columns["rndSortId"] == null)

dt.Columns.Add(new DataColumn("rndSortId", typeof(int)));

int rndSortColIdx = dt.Columns["rndSortId"].Ordinal;

for (int i = 0; i < dt.Rows.Count; i++)

dt.Rows[i][rndSortColIdx] = indices[i];

 

DataView dv = new DataView(dt);

dv.Sort = "rndSortId";

return dv;

}

}

posted @ 7:38 AM | Feedback (4)

Wednesday, April 09, 2008

For the attendees of tonight's user group talk in Waltham, you can grab the demos here. Thanks for coming, and for all the great questions!

posted @ 8:48 PM | Feedback (1)

Tuesday, April 01, 2008

I'll be speaking next Wednesday (April 9, 2008) at the Boston .NET User Group meeting in Waltham, MA. I'm going to look at the new control architecture in Silverlight 2 and talk about building reusable components in Silverlight, so if you're in the area, be sure to reserve the night - should be fun!

posted @ 8:32 AM | Feedback (1)

Monday, March 31, 2008

Here's a quick way to print the current row number in a Repeater control (I promised a conference talk attendee to post this recently):

<asp:Repeater ID="rep1" runat="server' DataSourceID="myds">
<ItemTemplate>
<%#(((RepeaterItem)Container).ItemIndex+1).ToString() %>
<-- other data binding expressions here... -->
</ItemTemplate>
</asp:Repeater>

posted @ 10:34 AM | Feedback (0)

Thursday, March 13, 2008

If you attended my talk today on New Features of ASP.NET 3.5 and Visual Studio 2008 at DevWeek 2008, you can grab the demos here.
Thanks for coming!
posted @ 7:29 AM | Feedback (4)

Wednesday, March 12, 2008

If you were at the user group meeting in London tonight for VBUG, here are the demos for the talk. Thanks for coming and for all the great questions!
posted @ 4:36 PM | Feedback (4)
 
If you attended my talk (or talks) today on Silverlight ASP.NET Controls and Silverlight Server Communication at DevWeek 2008, you can grab the demos here and here respectively.
Thanks for coming!
posted @ 7:53 AM | Feedback (2)

Tuesday, March 11, 2008

If you attended my talk today on ASP.NET Ajax Design at DevWeek 2008, you can grab the demos here.
Thanks for coming!
posted @ 7:22 AM | Feedback (2)

Monday, March 10, 2008

Thanks to all who attended today's pre-conference tutorial on Silverlight with myself and Ian Griffiths. You can grab the demos here.
Remember to stop by the Pluralsight booth if you were one of the t-shirt winners to collect your prize (and please stop by even if you weren't)!
posted @ 11:54 AM | Feedback (2)


 
   
 
© 2004 Pluralsight.
Visual Design by Studio Creativa
Privacy Policy