Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9081953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:26:27+00:00 2026-06-16T20:26:27+00:00

We would like to populate a DropDownList with a default value that is obtained

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:26:28+00:00Added an answer on June 16, 2026 at 8:26 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to populate a DateTime structure from a string, but it is
I have a view that I would like to populate data when the next
I have a MySQL database which contains data i would like to populate into
I have two divs. I would like to move/populate the text from div id
I would like to add items to a dropdownlist progmatically. I have an empty
I have a continuous form that i would like to populate via a sql
I would like to populate a combobox with the following: Visible item / Item
I would like to pre-populate a UITextfield with the phone number of the current
I am working on a children's book app and would like to dynamically populate
I am working on a children's book app and would like to dynamically populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.