Thursday, May 5, 2011

DropDownList binding problem

Hi,

I have two DropDownListBoxes one is called ddlDay and the other is ddlMonth. As their names suggests, ddlDay has values from 01 to 31 (not dynamic) and ddlMonth has values from 01 to 12. These values are not dynamically set.

When the page loads, I am getting values from the db.. depending on the value, I am using ddlDay.SelectedItem.Value = the value from the db to set the value and make the item selected.

The code I provide below was tested with If Not Page.IsPostback Then... statement without any luck.

however, when the page is loaded for the first time, nothing happens. but if I refresh the page and therefore cause a postback, then the values get binded correctly.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        loadInterview()
    End Sub

Private Sub loadInterview()
    If Request.QueryString("iID") = "" Or Request.QueryString("iID") Is Nothing Then
        Response.Redirect("interviews_list.aspx")
    Else
        Dim int As New hh.Interviews
        int = hh.Interviews.ReturnSingleInterview(Request.QueryString("iID"))

        ddlDay.SelectedItem.Value = int.InterviewDate.Day.ToString("00")
        ddlMonth.SelectedItem.Value = int.InterviewDate.Month.ToString("00")
        txtYear.Text = int.InterviewDate.Year

        txtPerson.Text = int.InterviewPerson
        txtTitle.Text = int.InterviewTitle
        txtText.Text = int.InterviewText

    End If
From stackoverflow
  • Try this to set initial values :

    ddlDay.ClearSelection()
    ddlDay.Items.FindByValue(int.InterviewDate.Day.ToString("00")).Selected = True
    
    ddlMonth.ClearSelection()
    ddlMonth.Items.FindByValue(int.InterviewDate.Month.ToString("00")).Selected = True
    

    NOTE : ClearSelection method avoids the HttpException exception with 'Cannot have multiple items selected in a DropDownList.' message.

    EDIT 2 : Suppose you have this dropdown list :

    <asp:DropDownList ID="ddlItems" runat="server">
        <asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem>
        <asp:ListItem Text="Item 2" Value="Item 2" Selected="true"></asp:ListItem>
        <asp:ListItem Text="Item 3" Value="Item 3"></asp:ListItem>
    </asp:DropDownList>
    

    If you set "Item 1" to your ddlItems.SelectedItem.Value like that :

    ddlItems.SelectedItem.Value = "Item 1"
    

    Your dropDown list becomes like that :

    <asp:DropDownList ID="ddlItems" runat="server">
        <asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem>
        <asp:ListItem Text="Item 2" Value="Item 1" Selected="true"></asp:ListItem>
        <asp:ListItem Text="Item 3" Value="Item 3"></asp:ListItem>
    </asp:DropDownList>
    

    I mean ddlItems.SelectedItem refers to your dropdown's selected item.

    Chad Grant : that exception was so annoying. I think they fixed it so it doesn't throw that exception anymore, or maybe I got trained to always unset the selection first.
    Emin : thanks, it worked. But I do wander the difference of SelectedItem.Value and Items.FindByValue

0 comments:

Post a Comment