开发者

Problem with GridView, Bind() and DropDownList

I am updating an existing ASP .NET site. This site has a custom grid control class that extends the GridView control to add a few features. Many pages in the site use the built in declarative two-way binding feature that is built into the base GridView, i.e. colu开发者_JS百科mn templates make calls to Bind() so that data can be shown and updated automatically. This works fine in most cases. However, when binding DropDownList controls there is now a problem.

Recently I had to add a feature that allows records in some tables to be marked as 'Closed', i.e. they can no longer be referenced by new records being inserted into other tables.

When editing a data grid row that has a DropDownList of child records that can be closed, the SelectedValue property might be bound to an ID that does not exist in the list. This causes an ArgumentOutOfRange exception to be thrown. I just want the DropDownList to default to no selection if the record is closed and therefore not in the list.

I'm looking for the easiest way to solve this. If possible I don't want to have to make a lot of changes to existing code.

I can programmatically set the selected index of the DropDownList in the RowDataBound event. But I can't find a way of updating the value whilst keeping the existing update functionality.

The actual question:

Is there some way to extend the DropDownList to make it ignore invalid values for the SelectedValue property? The only example I have seen so far does not work. I think that the DropDownList caches the value in case it has not yet had its DataSource property set, so overriding the SelectedValue property is not sufficient.

Alternatively, if there is a way to use the OnRowUpdating event to manually add the data to the update then that would be OK. I have tried adding values to the NewValues dictionary on the GridViewUpdateEventArgs class but it doesn't seem to work. Note that the grids are bound to lists of objects, not DataSourceControl derived controls.

Any help would be appreciated. Thanks.


If anyone's interested, I think I solved this by overriding the PerformDataBinding method and catching the ArgumentOutOfRangeException there. I suspect that the SelectedValue property might need to be overridden as well if the order in which the two properties are bound can vary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜