如何将值传递到下拉列表以在编辑模板上突出显示值?

时间:2013-12-26 17:23:02

标签: asp.net vb.net formview dropdownbox

表单中的标签lblTempLib具有来自表A的数据。这需要作为编辑模板中数据源的选定值传递到下拉列表。

这是LibDS(标记)的对象数据源:

SelectCommand="SELECT [LibName] FROM [tblBuilding]"></asp:SqlDataSource>

<asp:FormView ID="frmUpdateIncident" runat="server" DataSourceID="InciDetailDS" OnDataBound="frmUpdateIncident_DataBound">
    <EditItemTemplate>
        <asp:Label runat="server" ID="lblLib" Text="Library:" CssClass="style_bold"></asp:Label><br />
        <asp:Label ID="lblTempLib" runat="server" Text='<%# Eval("Library")%>'Visible="true"></asp:Label>
        <asp:Dropdownlist runat="server" ID="ddLib" DataTextField="LibName" DataSourceID="LibDS" >
        </asp:Dropdownlist>

VB.NET代码:

Protected Sub frmUpdateIncident_DataBound(sender As Object, e As EventArgs) Handles frmUpdateIncident.DataBound
    Dim ddLib As DropDownList 
   Try
        If Page.FindControl("ddLibrary") IsNot Nothing Then
            ddLib = DirectCast(frmUpdateIncident.FindControl("ddLibrary"), DropDownList)
            ddLib.Items.FindByText(strLibName).Selected = True
        End If
 Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

好的,我终于明白了。需要将索引值传递给下拉列表SelectedIndex值,因为此属性可以获取并设置与SelectedValue不同的值。 strLibName是前一个表单传递的值。

我在这里硬编码ddLib.SelectedIndex来测试它,但我将通过传递列表索引值来修复它。 VB.Net代码:

    Protected Sub frmUpdateIncident_DataBound(sender As Object, e As EventArgs) Handles frmUpdateIncident.DataBound
    If frmUpdateIncident.CurrentMode = FormViewMode.Edit Then
        Dim ddlib As DropDownList
        ddlib = DirectCast(frmUpdateIncident.FindControl("ddLibrary"), DropDownList)
        If ddLib IsNot Nothing Then
            If ddLib.Items.Count > 0 Then
                If strLibName IsNot Nothing Then
                    ddlib.SelectedIndex = 2
                    Dim strtest As String = ddlib.SelectedValue.Trim
                End If
            End If
        End If
    End If
End Sub