关于在FormView中的edit模式下查找SqlDataSource的问题?
程序中用到一个FormView1,在它的编辑模式下有一个下拉列表控件DropDownList1,
DropDownList1的邦定表达式:Bind( "name_id ")。
DropDownList1的数据源为一个SqlDataSource1(与DropDownList1都在edit模板内)。
SqlDataSource1的SQL为(SELECT dd_ID, dd_NAME FROM DXZ_TABLE WHERE (dd_ZB = @dd_ZB))
DropDownList1的DataTextField为dd_namae
DropDownList1的DataValueField为dd_id
==========================================================================
现在我要再FormView1进入编辑模式时对其中的DropDownList1所连接的SqlDataSource1进行过滤参数的赋值,于是我在FormView1_DataBound事件中加入如下代码:
Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
'数据连接
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings( "DDB ").ConnectionString())
If (FormView1.CurrentMode = FormViewMode.Edit) Then
Dim dzbCommand As SqlCommand
Dim dzbConnectionState As ConnectionState = conn.State
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
dzbCommand = New SqlCommand( "SP_dd_ID ", conn)
dzbCommand.CommandType = CommandType.StoredProcedure
dzbCommand.Parameters.Add( "@ddname ", SqlDbType.NVarChar, 50)
dzbCommand.Parameters.Item(0).Value = y_namev
Dim DXZ_DSs As SqlDataSource = CType(FormView1.FindControl( "SqlDataSource1 "), SqlDataSource)
DXZ_DSs.SelectParameters( "dd_ZB ").DefaultValue = CStr(dzbCommand.ExecuteScalar)
Finally
If dzbConnectionState = ConnectionState.Close