1、在formview控件模板列中放入两个DropDownList控件
2、将第一个控件按常规方法加入数据源,设置要显示的字段名和字段值,并与formview中的相应字段关联,关设置为启用autopostback
3、给第二个DropDownList控件加入数据源,设置要显示的字段名和字段值,在其DataBound事件中加入如下代码
Protected Sub DropDownList2_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
'指定下拉框的父控件
Dim frmv As FormView = CType(ddl.NamingContainer, FormView)
If Not frmv.DataItem Is Nothing Then
Dim strcity As String = CType(frmv.DataItem, DataRowView)("SmallClassID")
'清除下拉框的选择项
ddl.ClearSelection()
'在下拉框的列表项中找到值
Dim li As ListItem = ddl.Items.FindByValue(strcity)
If Not li Is Nothing Then
li.Selected = True
End If
'找到控件DropDownList2
ddl = CType(frmv.FindControl("DropDownList2"), DropDownList)
'邦定数据
If Not ddl Is Nothing Then
ddl.DataBind()
End If
End If
End Sub
4、给formview的ItemInserting事件加入如下代码:
'当触发formview的插入数据的项时进行下列操作
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
将DropDownList2的值赋给formview控件中的插入参数SmallClassID()
Dim strprov As Int32 = CType(CType(sender, FormView).FindControl("DropDownList2"), DropDownList).SelectedValue
e.Values("SmallClassID") = strprov
End Sub