0

当用户从 VB.Net 代码隐藏文件进入 ASP.Net DetailsView 上的“添加”模式时,我们希望使用从变量获得的默认值填充 DropDownList。你能告诉我如何让它填充吗?

这是我们希望填充的 DropDownList 的标记:

<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
    <EditItemTemplate>
        <asp:DropDownList 
            ID="DropDownListClass" 
            Runat="server"
            DataSourceID="SqlDataSourceClasses"
            DataTextField = "ClassName"
            DataValueField="ID"
            SelectedValue='<%# Bind("ClassID") %>'
            ForeColor="Blue">
        </asp:DropDownList>

        <asp:RequiredFieldValidator ID="RequiredFieldValidatorEditClass" runat="server" ControlToValidate="DropDownListClass" 
            ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
            SetFocusOnError="True" Display="Dynamic">

        </asp:RequiredFieldValidator>

    </EditItemTemplate>

    <InsertItemTemplate>

        <asp:DropDownList 
            ID="DropDownListClass" 
            Runat="server"
            DataSourceID="SqlDataSourceClasses"
            DataTextField = "ClassName"
            DataValueField="ID"
            SelectedValue='<%# Bind("ClassID") %>'
            ForeColor="Blue">
        </asp:DropDownList>

        <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClass" 
            ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
            SetFocusOnError="True" Display="Dynamic">

        </asp:RequiredFieldValidator>
    </InsertItemTemplate>

    <ItemTemplate>
        <asp:DropDownList 
            ID="DropDownListClass" 
            Runat="server"
            DataSourceID="SqlDataSourceClasses"
            DataTextField = "ClassName"
            DataValueField="ID"
            SelectedValue='<%# Bind("ClassID") %>'
            Enabled="false"
            ForeColor="Blue"
            Font-Bold="true"> 
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

我们在代码隐藏文件中使用它来填充将包含默认值的变量:

Function GetValueFromDropDownListClassItem() As String

    Dim strValueToReturn As String
    Dim drpValue As DropDownList

    drpValue = DetailsView.FindControl("DropDownListClass")

    If String.IsNullOrEmpty(drpValue.Text) Then
        strValueToReturn = ""
    Else
        strValueToReturn = drpValue.SelectedItem.Text
    End If

    Return strValueToReturn
End Function

我们只想使用该值,并使用该变量中的值预先选择 DropDownList。

4

1 回答 1

2

编辑:

你的意思是这样吗?

Protected Sub dropdown_DataBound(sender As Object, e As EventArgs)
    Dim list As DropDownList = TryCast(sender, DropDownList)
    Dim value as String = GetValueFromDropDownListClassItem()
    If list IsNot Nothing And value IsNot "" Then
        list.SelectedValue = value
    End If
End Sub

旧消息:请尝试以下操作:

<asp:DropDownList 
    ID="DropDownListClass" 
    Runat="server"
    DataSourceID="SqlDataSourceClasses"
    DataTextField = "ClassName"
    DataValueField="ID"
    SelectedValue='<%# Bind("ClassID") %>'
    AppendDataBoundItems="True"
    ForeColor="Blue">
        <asp:ListItem Selected="True" Value="0">Please select</asp:ListItem>
</asp:DropDownList>

请注意 AppendDataBoundItems true 和 listItem

于 2013-01-04T15:14:01.773 回答