Tuesday, February 20, 2007

ASP.NET 2.0 ObjectDataSource Madness

Ok so after several days and nights of torment, I have learnt quite a lot about GridView controls and binding (or not, as the case may be) to them.


You thought with .NET 2.0 that all your data binding woes had been answered. Oh how very wrong you were, you poor, poor developer (emphasis on poor; these are hard times, what with house prices soaring and whatnot).

It is true to say that you can use an ObjectDataSource in conjunction with a GridView control to present data to the user in a nice tabular format, though you are not likely to achieve this without a) pain b) coffee and c) (in extreme cases) a near-death experience.

Here are some brief and scattered notes on my experiences whilst attempting to use Object Data Sources:

- Update methods can get easily out of synch. There is a hack-around/fix for this - use the ODS 'OnUpdating' method, like so:

//protected void DtypesODS_Updating(object source, ObjectDataSourceMethodEventArgs e)
{
// Modify input parameters because ODS is too dumb to figure it out.
if (e.InputParameters.Contains("DType"))
e.InputParameters.Remove("DType");
}
And obviously add a ref to that in your aspx, or initialise the event by overriding OnInit.

[More to come.]

Thursday, February 15, 2007

Using A Class From A Seperate Assembly With An ObjectDataSource

First, add a reference to the assembly at the top of the page:

<%@ Register TagPrefix="MyAssembly" Namespace="MyAssembly.Data" Assembly="MyAssembly, Version=1.0.0.1, Culture=neutral, PublicKeyToken=zj1251fcc32b5cg6" %>


Now set the ObjectDataSource's "TypeName" property to the specific class:

TypeName="MyAssembly.Data.MyDataClass"

Now right click on the object data source in design view and select 'Configure Data Source' and you'll see the methods on your class to choose from.





Friday, February 09, 2007

ASP.NET 2.0 - Some Basic Things To Remember

- When you want to use simple binding with no evaluation:

# Bind("MyFieldName")

- When you want to use evaluation, for example a value against a protected method, use:

# IsLead(Eval("Lead"))

Code behind:
protected string IsLead(object val)
{
if (val.ToString() == "True")
return "Yes";
else
return String.Empty;
}

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Z