DropDownList in a FormView Control

Another in an apparently relentless series of posts about trying to see how much can be done with the new data controls in ASP.NET 2.0 without writing code. It's pretty easy to take advantage of the code hooks we all know and love, like the ItemDataBound event in the DataGrid control ( RowDataBound in the GridView control). which lets you do a whole lotta stuff. A more interesting challenge is to see what you can do with no code at all.(referred rom mike's weblog)[@more@]A question came up several times the other day about using a DropDownList control inside a (e.g.) FormView control. It's a pretty common scenario -- people want to be able to edit values by picking a value from a list:

FormView_DropDown.gif

In this example, the FormView control displays data from the Northwind Products table. One of the fields is CategoryID. The easy way to edit the CategoryID field is to pick from a list of categories. So the FormView control's templates contain a DropDownList control that displays records from the Category table. Make sense?

Once again the clever Polita (who owns the FormView control) was able to whip this thing out on her whiteboard in about 60 seconds. This, I should probably add, after I myself had, mmm, not been able to do that.

The first job is to display the category name in the read-only ItemTemplate. You can do that a couple of ways. One way would be to create a Select statement that joined Products to Categories and thereby gave you access to CategoryName. You could then display it using a Label control or something.

Here I practiced, so to speak, using the DropDownList control. The FormView and DropDownList controls, as I hope is clear, display data from two different tables. The easy way to set that up is to use two data source controls -- one to get the Products data, and a second one to get the Category data. The FormView control is bound to the first data souce control, no problem. In the ItemTemplate, you add a DropDownList and configure it something like this:
   DataSourceID="AccessDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID"
SelectedValue=''
Enabled="False" />
The two interesting things here are 1) we set the SelectedValue property by data binding it to the current CategoryID value. The DropDownList sorts out (haha) how to use that value to display the right category name. 2) We're using Eval, because the ItemTemplate is read only.

And it turns out -- this was the surprising part, maybe -- that is isn't any harder to use a DropDownList control in the EditItemTemplate, where it functions as a category picker. Here's the declaration:
   DataSourceID="AccessDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID"
SelectedValue='' />
Same deal, except that in this case, we use Bind for the data binding method in order to get two-way binding. The whole entire page is below, including the data source controls (Access, in this case). (It's not formatted at all to try to keep the listing to a reasonable size.)

br /> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




FormView with DropDownList




DataFile="~/App_Data/northwind.mdb"
SelectCommand="SELECT [ProductID], [ProductName],
[CategoryID] FROM [Products]"
UpdateCommand="UPDATE [Products] SET [ProductName] = ?,
[CategoryID] = ? WHERE [ProductID] = ?">








runat="server"
DataFile="~/App_Data/Northwind.mdb"
SelectCommand="SELECT [CategoryID], [CategoryName]
FROM [Categories]">





DataKeyNames="ProductID"
DataSourceID="AccessDataSource1"
AllowPaging="True">

ProductID:
Text=''/>


ProductName:
Text='' />


CategoryID:
DataSourceID="AccessDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID"
SelectedValue='' />


( runat="server"
Text='' />)


CausesValidation="True"
CommandName="Update"
Text="Update" />
CausesValidation="False"
CommandName="Cancel"
Text="Cancel" />



ProductID:
Text='' />


ProductName:
Text='' />


CategoryID:
DataSourceID="AccessDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID"
SelectedValue=''
Enabled="False" />
( Text=''/>)




CausesValidation="False"
CommandName="Edit"
Text="Edit" />





来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10771986/viewspace-969547/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10771986/viewspace-969547/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值