Strongly-Typed Binding for List Controls
page 4 of 5
by J. Ambrose Little
Average Rating: 
Views (Total / Last 10 Days): 24463/ 62

Sample Usage

In the sample project (Download Here), you can see two sample usages of this architecture, a simple example without adding paramters (Listing 4) and the more complex usage subscribing to the BuildingParameters event (Listing 5).  Observe how the LoadParameters method is added as an event handler for the BuildingParameters event in Listing 5.  It is a bit cumbersome to create a method when we could just have one line of code (in this case); doing this in-line will be possible with anonymous methods coming in Whidbey.

Listing 4

1:     private void LoadStates()

2: {
3: // Simple Retrieval -- No Parameters Needed
4: NameValuePairCollection states =
5: new NameValuePairCollection();
6: // Fill From Db
7: states.FillFromDb(MenuCommands.StateMenu,
8: MenuCommands.DefaultConnectionString);
9: // Bind to List Control
10: states.BindToListControl(this.DropDownList1);
11: }

Listing 5

1:     private void LoadAuthors()

2: {
3: // Complex Retrieval -- Need to Add Parameters
4: NameValuePairCollection authors =
5: new NameValuePairCollection();
6: // Add Event Handler for Building Parameters
7: // (Anonymous methods will help here)
8: authors.BuildingParameters +=
9: new NameValuePairCollection.BuildingParametersHandler(
10: // Fill From Db
11: authors.FillFromDb(MenuCommands.AuthorByStateMenu,
12: MenuCommands.DefaultConnectionString);
13: // Bind to List Control
14: authors.BindToListControl(this.DropDownList2);
15: }
17: private void LoadParameters(object sender,
NameValuePairCollection.BuildingParametersArgs e)
18: {
19: // Compare on the command name to enable reuse of this method
for more than one

20: // command/stored proc
21: if (e.CommandName == MenuCommands.AuthorByStateMenu)
22: {
23: // Create the Parameters for the dbo.GetAuthorMenuByState

24: // State Paramter
25: e.Parameters.Add(
26: new System.Data.SqlClient.SqlParameter("@State",
27: this.DropDownList1.SelectedValue));
28: }
29: }
You may notice the use of the MenuCommands type.  This is just an extra I added on at the last minute to give me strongly-typed options for specifying the different stored procedure names and database connections to be used in conjunction with with the LoadFromDb method calls.

Also note that for the sample to run, you'll probably need to update the connection string.  It is currently set to use integrated authentication to the default local SQL Server instance, and the application is set to use Windows authentication with impersonation (with all the gotchas that this implies).

View Entire Article

User Comments

Title: Well done. Very useful   
Name: Leo
Date: 2009-02-24 8:31:10 AM
Hi J. Ambrose Little,

Thanks for your code. I found it very useful indeed.
I used it in a scenario where I needed to create a NameValuePair Collection to concatenate two fields before displaying it on a DropDownList. Without your approach I would have to retrieve an concatenate column field just for my business object in one specific dropdown.

I am using CSLA as framework for the Business Objects. I am looking forward to make it generic with my objects as is not suitable for me use your FillFromDb, because this data is already manipulated by my objects in a business layer.
Unfortunatelly I could not understand the BuildingParamters as I found quite hard to understand events with delegate, etc.

If you have experience with CSLA and have any suggestion to make the Fill up in a generic manner, please let me know.

Thanks a lot.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-22 7:06:49 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search