webcam porn

Archive for May, 2006

Passing an object to ObjectDataSource

Wednesday, May 10th, 2006 | .NET | 4 Comments

I like the ObjectDataSources in some occasions, even if liked the .NET 1.1 way of working with data better. I have played around with ObejctdataSources for a while, and it hit me that it's not intuitive to pass an object as the parameter.  The Visual Studio designer has functionality for passing parameters to the business objects, but only as strings. This is actually a limitation to the Parameter class as the DefaultValue is of type string.

Coding standards suggest that a method should have 7 parameters or less (some say even 5), and databinding methods can have a lot of parameters based on what data to fetch. A good way is to pass a struct or object instead of several parameters.

So it's not possible to initially pass an object as a parameter. You have to do it in an event instead. The following solution illustrates how it could be done:

  • Select the ObjectDataSource in the designer and select the Events for the ObjectdataSource.
  • Double click the Selecting event. This generates a callback method for the event in your code behind file.
  • The method should look something like this:

protected void ObjectDataSourceNewsList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
NewsArchive newsArchive = new NewsArchive();
newsArchive.Language = "no";
newsArchive.Country = "NO";
e.InputParameters["newsArchive"] = newsArchive;
}

The newsArchive parameter is defined in the ObjectDataSource as of type Object. This works fine, but it's rather strange that you have to define the parameter in an event and not by the parameters in the ObjectDataSource.

Tags: