Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging DropDown Selected Item Based on Selected Value (ASP.NET)
    text
    copied!<p>I have a dropdown list on my page (ddlProgram) which is populated via a database query like so:</p> <pre><code>Using dbContext as IRFEntities = New IRFEntities Dim getPrograms = (From p in dbContext.IRF_Program _ Order By p.name _ Select p) ddlProgram.DataSource = getPrograms ddlProgram.DataTextField = "name" ddlProgram.DataValueField = "id" ddl.Program.DataBind() End Using </code></pre> <p>So, for example, one might have a DataTextField of "Education" and an ID of "221".</p> <p>Now, I prepopulate the form with information about the individual visiting the site (if available) - including the dropdown list like so:</p> <pre><code>If getProspect IsNot Nothing Then If getProspect.user_id Is Nothing Then ddlProgram.SelectedValue = getProspect.Program End If End If </code></pre> <p>The Program property contains a number that matches the ID of a Program. So, for example, this individual might have a Program of "221" which would match the "221" of Education mentioned above.</p> <p>Currently the application successfully sets the SelectedValue to "221" for the DropDownList (ddlProgram), but the SelectedItem of the DDL remains the same (e.g., if it is initially "History" with an ID of "1" after the prepopulation it is "History" with an ID of "221").</p> <p>What I'm trying to make happen is that the SelectedItem is updated to item which corresponds with the SelectedValue. So, in the end, if the individual has "221" for "Education" selected when the form is prepopulated they would see Education as the selected item and the selected value would be set correctly, whereas right now the form is showing the wrong SelectedItem but has the right SelectedValue behind the scenes.</p> <p>Here is a more complete idea of the code flow from the Page_Load event:</p> <pre><code> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Page.IsPostBack = False Then ' If prospect is coming from unique url Dim prospect_url As String = Page.RouteData.Values("value") ' Save prospect_url into session variable Session("prospect_url") = prospect_url Using dbContext As IRFEntities = New IRFEntities ' Prepopulate the programs dropdown. Dim getPrograms = (From p In dbContext.IRF_Program _ Order By p.name _ Select p) ddlProgram.DataSource = getPrograms ddlProgram.DataTextField = "name" ddlProgram.DataValueField = "id" ddlProgram.DataBind() End Using Using dbContext As IRFEntities = New IRFEntities ' Prepopulate the states dropdown. Dim getStates = (From p In dbContext.IRF_States _ Order By p.name _ Select p) ddlState.DataSource = getStates ddlState.DataTextField = "name" ddlState.DataValueField = "id" ddlState.DataBind() End Using Using dbContext As IRFEntities = New IRFEntities ' Grab info. about prospect based on unique url. Dim getProspect = (From p In dbContext.IRF_Prospects _ Where p.url = prospect_url _ Select p).FirstOrDefault ' If they have a record... If getProspect IsNot Nothing Then If getProspect.user_id Is Nothing Then ' Prepopulate the form with their information. ' These must have a value, so we need to make sure that no column is null in the database. ddlProgram.SelectedValue = getProspect.program txtFirst.Text = getProspect.first_name txtLast.Text = getProspect.last_name txtAddress.Text = getProspect.address txtCity.Text = getProspect.city ddlState.SelectedValue = getProspect.state txtZip.Text = getProspect.zip txtPhone.Text = getProspect.phone txtEmail.Text = getProspect.email_address txtYearEnrolling.Text = getProspect.enrolling_in Else ' Redirect them to login. Response.Redirect("login.aspx") End If End If End Using End If End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload