ASP.NET DataSource和GridView数据绑定,以编程方式设置参数值

时间:2021-12-13 03:26:54

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

所以,我有一个带有ObjectDataSource的GridView,我想以编程方式设置ObjectDataSource的SelectParameters之一。

I tried (during both Page_Load and DropdownList__SelectedIndexChanged)

我试过(在Page_Load和DropdownList__SelectedIndexChanged期间)

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

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

但它不起作用。你会建议什么?

2 个解决方案

#1


Trap the onselecting event on the datasource.

捕获数据源上的onselecting事件。

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

#2


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

然后确保在某处将GridView数据绑定

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

#1


Trap the onselecting event on the datasource.

捕获数据源上的onselecting事件。

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

#2


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

然后确保在某处将GridView数据绑定

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