ASP.NET DataSource & GridView databinding, setting a parameter value programmatically
-
20-08-2019 - |
Question
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?
Solution
Trap the onselecting event on the datasource.
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["month"] = DateTime.Now.Month;
}
OTHER TIPS
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow