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 948367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:14:04+00:00 2026-05-15T23:14:04+00:00

I have the following code in my codebehind Page_Load function that sets the default

  • 0

I have the following code in my codebehind Page_Load function that sets the default selected value of a dropdownlist in detailsview based on the name of a record returned from a sql data query.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Page.Title = "Editing record"

        'Perform dropdown list population operations
        Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown")
        If Page.IsPostBack = False Then

            Dim ticket_ID As String = getDataKey(DetailsView1)
            'Fetch Category ID
            Dim sqlText As String = "SELECT TS_REQCATEGORY FROM USR_ITFAC WHERE (TS_ID = " + ticket_ID + ") "
            Dim reqDataReader As SqlDataReader = GetDataReader(sqlText)
            reqDataReader.Read()

            Dim category_ID As String = reqDataReader(0)

            'Fetch Category name and set as selected value in dropdown list
            sqlText = "SELECT TS_NAME FROM TS_SELECTIONS WHERE (TS_ID = " + category_ID + ") "
            reqDataReader = GetDataReader(sqlText)
            reqDataReader.Read()
            category_Name = reqDataReader(0)
            'myDDL.DataBind()
            myDDL.SelectedValue = category_Name
        End If

    End Sub

My problem is that when the page loads for the first time, even though I set the selected value for the dropdownlist it will not display and instead simply displays the default first name in my dropdownlist. I tried Binding my dropdownlist before and after I set the selectedvalue, it is commented out in the sample code above, but that didn’t seem to do anything.

UPDATE:
I’m setting the data source in the webform as follows:

Dropdownlist:

<asp:DropDownList DataSourceID="ReqCategoryData" DataTextField="ReqCategory" DataValueField="ReqCategory"
                                ID="reqCategoryDropDown" runat="server" AppendDataBoundItems="true" AutoPostBack="true">                                                            
                            </asp:DropDownList>

Connects to data source a few lines down:

<asp:SqlDataSource ID="ReqCategoryData" runat="server" ConnectionString="<%$ ConnectionStrings:TTPRODReportsQuery %>"
        SelectCommand="SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME">
        </asp:SqlDataSource>

UPDATE_2:
Ok, so I implemented he SQLDataSource programmatically in the code-behind under the Page_Init function as follows:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        DetailsView1.DefaultMode = DetailsViewMode.Edit

        ''Setup DropDownList SqlDataSource
        ddlDataSource.ID = "ReqCategoryData"
        Page.Controls.Add(ddlDataSource)
        ddlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("TTPRODReportsQuery").ConnectionString
        ddlDataSource.SelectCommand = "SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME"
        Dim args As New DataSourceSelectArguments
        ddlDataSource.Select(args)
        ddlDataSource.DataBind()

    End Sub

After which I set attempt to set the selected value of the dropdownlist as above. The page is still not setting the selected value.

  • 1 1 Answer
  • 1 View
  • 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-05-15T23:14:04+00:00Added an answer on May 15, 2026 at 11:14 pm

    Perhaps check these items:

    • ensure you’re calling myDDL.DataSource = reqDataReader or some other form of setting the DataSource of your dropdownlist.
    • ensure the dropdownlist has its DataTextField and DataTextField set properly. This could be in code-behind or in markup.
    • The 0’th index of reqDataReader — are you sure that’s the intended ordinal for the DataValueField?

    Perhaps something like this:

    With myDDL
        .DataTextField = "CategoryName"
        .DataValueField = "CategoryID" ' or perhaps CategoryName as your value.
        .DataSource = reqDataReader
        .DataBind()
        .SelectedValue = category_Name ' Name or ID; depends on what was set as DataValueField
    End With
    

    DataBinding – Code-Behind vs. Markup DataSources

    Consider choosing one style of binding your dropdownlist – in code-behind or with a markup SqlDataSource. Having both generates confusion on which bind wins – the code-behind or the datasource. I think it’s the SqlDataSource in this case.

    Consider removing the SqlDataSource from out of the markup, and create a method in code-behind whose sole purpose is to create the data binding. This is the only place where the binding should happen. If you ever need to track down a defect or enhance your logic, it’s the only one place to visit.

    Private Sub BindMyDropDown(Optional ByVal selectedValue as String)
        With myDDL
            .DataTextField = "CategoryName"
            .DataValueField = "CategoryID" ' or perhaps CategoryName as your value.
            .DataSource = LoadTicketReqCategory(TicketID)
            .DataBind()
            .SelectedValue = selectedValue ' Name or ID; depends on what was set as  DataValueField
        End With
    
    End Sub
    

    From your Page_Load(), check for IsPostBack() and call this method when you aren’t posting back.

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

Sidebar

Related Questions

I have following code that I am compiling in a .NET 4.0 project namespace
I have following simple code: <%@ Page Language=C# AutoEventWireup=true CodeBehind=testForm.aspx.cs Inherits=Orbs.testForm %> <html> <body>
I have the following javascript function to validate the usercode value entered in the
I have apsx page that has the following simple code: <%@ Page Title= Language=C#
I have the following code behind a button in Visual Studio 2010 private void
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method
I have following code for inserting data into database using PDO. It inserts data

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.