【问题标题】:Populating ASP.Net DetailsView DropDownList with default value when entering Add mode进入添加模式时使用默认值填充 ASP.Net DetailsView DropDownList
【发布时间】:2012-12-19 00:58:40
【问题描述】:

当用户从 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。

【问题讨论】:

    标签: asp.net vb.net default-value populate code-behind


    【解决方案1】:

    编辑:

    你的意思是这样的?

    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

    【讨论】:

    • 感谢您的回复,但我们不希望添加新值,而是选择从包含要选择的值的变量获得的 DropDownList 中的现有值。
    • 类似于我在编辑中发布的内容?
    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多