DropDownList binding problem

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

When the page is loaded, I get the values ​​from db .. depending on the value, I use the ddlDay.SelectedItem.Value = the value from the db to set

value and select the item.

The code below has been tested using the operator If Not Page.IsPostback Then...

with no luck.

however, when the page is loaded for the first time, nothing happens. but if I refresh the page and therefore call a postback, then the values ​​are bound 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

      

0


source to share


1 answer


Try setting 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. The ClearSelection method allows you to throw HttpException by using "Cannot select multiple items in DropDownList". message.

EDIT 2: Let's say you have this dropdown:

<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 this:

ddlItems.SelectedItem.Value = "Item 1"

      

The dropDown list will look like this:

<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 selected item.

+1


source







All Articles