根据选定值更改DropDown选定项(ASP.NET)

时间:2012-10-24 15:32:40

标签: asp.net vb.net

我的页面上有一个下拉列表(ddlProgram),它通过数据库查询填充,如下所示:

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

因此,例如,可能有一个DataTextField" Education"和#34; 221"。

现在,我预先填写表格,其中包含有关访问该网站的个人信息(如果有) - 包括下拉列表,如下所示:

If getProspect IsNot Nothing Then
  If getProspect.user_id Is Nothing Then
    ddlProgram.SelectedValue = getProspect.Program
  End If
End If

Program属性包含一个与程序ID匹配的数字。因此,例如,这个人可能有一个" 221"这将匹配" 221"上面提到的教育。

目前,应用程序已成功将SelectedValue设置为" 221"对于DropDownList(ddlProgram),但DDL的SelectedItem保持不变(例如,如果它最初是"历史" ID为" 1"在预填充之后它是&# 34;历史" ID为" 221")。

我尝试做的是将SelectedItem更新为与SelectedValue对应的项目。所以,最后,如果个人有" 221"为"教育"在预填充表单时选择它们会将Education视为所选项目,并且所选值将正确设置,而此时表单显示错误的SelectedItem但在幕后具有正确的SelectedValue。

以下是对Page_Load事件代码流的更完整概念:

    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

1 个答案:

答案 0 :(得分:2)

你在做什么看起来应该有效。如果在设置值后设置断点并检查SelectedItem文本和值,它们是否显示为预期或不匹配?

使用立即窗口检查:

ddlProgram.SelectedItem.Text
ddlProgram.SelectedItem.Value

如果它们看起来相同,那么我会假设绑定代码正在被重新生成,并且正在重新生成列表并选择第一个项目。

要检查这一点,请在绑定代码上设置一个断点,看看它是否被多次触发并正确更正方法的顺序。

增加: 如果它适用于您的本地环境,它应该在发布时工作,如果代码相同?看看你的代码,我首先将一些数据绑定代码分离成单独的方法,而不是把所有内容都放在Page_Load中,因为这是一个很好的做法和两个因为它会使调试变得更容易。除此之外,我不确定还有什么建议。