views:

1353

answers:

2

So, I have a GridView with an ObjectDataSource, and I want to programmatically set one of the SelectParameters of the ObjectDataSource.

I tried (during both Page_Load and DropdownList__SelectedIndexChanged)

objectDataSource.SelectParameters["my_parameter"].DefaultValue = "my_value";
objectDataSource.DataBind();

but it didn't work. What would you suggest?

+1  A: 

Trap the onselecting event on the datasource.

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) { e.InputParameters["month"] = DateTime.Now.Month; }

RubbleFord
A: 

Nevermind, I solved it myself.

In Page_Load:

objectDataSource.Selecting += new ObjectDataSourceSelectingEventHandler(objectDataSource_Selecting);

Then write the handler method:

void objectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    int four = 2 + 2;
    e.InputParameters["my_parameter"] = four;
}

Then make sure to databind the GridView somewhere

protected void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    gridView.DataBind();
}
Gudmundur Orn