Demo for 2-way databinding cascading lists within a FormView

Demo for 2-way databinding cascading lists within a FormView

FormView in Edit mode
ID 4 Original Values
Company
Street Address
Country Netherlands
Province/State North Holland
City Enkhuizen
Update Cancel
12345

You might have wanted to use inside a databound templated server control (e.g. the FormView or the GridView) several cascading dropdownlists. The listitems in each dropdownlist would change based on the selection of the previous dropdownlist, e.g. the city selections would change based on the province and the latter would change based on the country selection. Sounds easy? Well, you added to your webform a datasource object linked for each dropdownlist where the SelectParameters collection contains a ControlParameter pointing to the previous list.

But you encounter any of those 2 error messages:

  1. Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
  2. 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.

"What is the problem?", you ask. Well, it is not a bug by Microsoft's judgement (look at this Bug Report)

When you have a dropdownlist that is dependent on another within a FormView server control, it becomes populated on SelectedIndexChanged event of the first dropdown list. However, in this case the template was not rebound to the data, only the dropdown list. The FormView was reconstructed from the saved ViewState. This causes the error message: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".

The solution is to remove the 2-way databinding from the cascading dropdownlists and replace it with customized code during processing the ItemUpdating event.

Click on the Edit link (of the FormView display above) to activate the EditItemTemplate of the FormView. Change the country selection to watch the province/State and city selections change accordingly. Then you can view the same concepts applied to the GridView by clicking on the tab for the GridView above. The source code is available in 4 versions based on the type of datasource and the programming language that you use.

Notice that every button click on this demo posts the entire page back to the server and causes the entire screen to flash back. This distracts from the viewing experience and it has a cost on band width where the entire navigational controls are being posted back and refreshed. Compare this to the same sample but while using Atlas (the new technology from Microsoft).

Source Code #1: Source Code for this demo using ObjectDataSource bound to the FormView in C#
Source Code #2: Source Code for this demo using ObjectDataSource bound to the FormView in VB
Source Code #3: Source Code for this demo using SqlDataSource bound to the FormView in C#
Source Code #4: Source Code for this demo using SqlDataSource bound to the FormView in VB
WEBSWAPP_BLL.Demos.cs: Source Code for the Business Logic Layer that operates this site written in C#
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值