Search code examples
asp.netvb.netdefault-valuepopulatecode-behind

Populating ASP.Net DetailsView DropDownList with default value when entering Add mode


We would like to populate a DropDownList with a default value that is obtained from a variable when the user goes into "Add" mode on an ASP.Net DetailsView from a VB.Net code-behind file. Can you show me how to get it populated?

Here is the markup for the DropDownList we wish to populate:

<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>

We are using this in the code-behind file to populate the variable that will contain the default value:

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

We just want to use this value and have the DropDownList pre-selected with the value from this variable.


Solution

  • Edit:

    You mean like this?

    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
    

    Old message: Please try the following:

    <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>
    

    Please note the AppendDataBoundItems true and listItem