Jonas Stawski

Everything .NET and More

Passing objects to the Object Data Source as parameters

This Object Data Source is very nice to use and almost all examples you see out there use primitive types as parameters. The question arrives when you want to pass an object as a parameter. You can't do it declaretively, so you have to do it on the codebehind. I've done this a million times, I told myself. I have changed the value of a parameter dinamically before. All you have to do is handle the inserting, updating, deleting event of the ODS and change the ods.updateparameters[0].defaultvalue. Well there's a problem with that! The DefaultValue takes a string and we need to pass an object. How do you do it then? Well, I was on the right track. We need to handle the event and then use the ObjectDataSourceMethodEventArgs parameter to insert it this way:

protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
   Teacher teach = new Teacher();
   //Set the values for the teacher

   //Add it to the parameters
   e.InputParameters.Add("teach", Teacher);
}

Add comment

biuquote
Loading