first take a look at attachment pic to get a idea.

I have two dropDownLists.1st dropDownList ID is cboPrev(oPrev) and 2nd dropDownList ID is cboNext(oNext).
These both DropDownList will have the following 4 items (2014, 2015, 2016, 2017). User can click on dropdownlist and pick a year.


so for default item on dropDownList is "2014" but when user click on "2016" than the default first item is still "2014" - which is wrong

What I want:

So for default item on dropDownList is "2014" but when user click on "2016" than the default first item in dropDownList should be "2016". I want the defualt item in dropDownList what every user select. How can I do this in vb .net?

  Protected Sub dlCalendar_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
        ' Just populationg my header section
        Dim dDate As DateTime
            dDate = DateTime.Now

        If e.Item.ItemType = ListItemType.Header Then
            Dim oPrev As DropDownList = DirectCast(e.Item.FindControl("cboPrev"), DropDownList)
            Dim oNext As DropDownList = DirectCast(e.Item.FindControl("cboNext"), DropDownList)
            Dim dtYear As New DataTable()

            ' store iteams in DropDownList: 2014, 2015, 2016, 2017
            dtYear.Rows.Add(DateTime.Now.AddYears(-1).ToString("yyyy"), "01 Jan," & DateTime.Now.AddYears(-1).ToString("yyyy"))
            dtYear.Rows.Add(DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("dd MMM, yyyy"))
            dtYear.Rows.Add(DateTime.Now.AddYears(1).ToString("yyyy"), "01 Jan," & DateTime.Now.AddYears(1).ToString("yyyy"))
            dtYear.Rows.Add(DateTime.Now.AddYears(2).ToString("yyyy"), "01 Jan," & DateTime.Now.AddYears(2).ToString("yyyy"))

            'bind data
            oPrev.DataTextField = "year4"
            oPrev.DataValueField = "sValue"
            oPrev.DataSource = dtYear
            oNext.DataTextField = "year4"
            oNext.DataValueField = "sValue"
            oNext.DataSource = dtYear

            'left(mm yyyy) - middle(mm yyyy) - right(mm yyyy)
            'click on link and it stay that year
            DirectCast(e.Item.FindControl("lblLeft"), Label).Text = "<a style=color:Black href=Calendar.aspx?id=" & Request.QueryString("id") & "&SpecificMonth=" & dDate.AddMonths(-1).ToString("dd-MMMM-yyyy") & ">" & dDate.AddMonths(-1).ToString("MMMM yyyy") & "</a>"
            DirectCast(e.Item.FindControl("lblMiddle"), Label).Text = dDate.ToString("MMMM yyyy")
            DirectCast(e.Item.FindControl("lblRight"), Label).Text = "<a style=color:Black href=Calendar.aspx?id=" & Request.QueryString("id") & "&SpecificMonth=" & dDate.AddMonths(+1).ToString("dd-MMMM-yyyy") & ">" & dDate.AddMonths(+1).ToString("MMMM yyyy") & "</a>"

        End If
    End Sub

    Protected Sub RfreshData(ByVal sender As Object, ByVal e As System.EventArgs)
        'redirect by date thats why page can render the calendar according to user selection
        Response.Redirect("Calendar.aspx?id=" & Request.QueryString("id") & "&SpecificMonth=" + DirectCast(sender, DropDownList).SelectedValue)
    End Sub

Mmm the dropdownlist I think has some indexes which you can play around with to get what you want. But since you didn't say whether the selected year should remain the first selected even if the program restarts, and also you said you need to change the "default selected" I think you either want to re-arrange or want to just remember the selected value.

To do so you will need a global variable that will hold the selected value, but for your app to remember your choice you will need something to write your choice/selection(database/text file) at.

You can either keep/remember the index or the selected value, your choice.

 ' Suppose you want to keep the index. This is your global variable
 Dim SelectedIndx As Integer

Now you need to put this inside the dropdown selected item .

 SelectedIndx = DropDownList1.SelectedIndex

Now you will need 'IF' statement on your refresh to ensure the selected data remains selected.

 If SelectedIndx = Nothing Then
 ' Because we haven't saved any selected the default will apply.
 DropDownList1.SelectedIndex = 0
 ' We have stored something so let's replace default and use our own default selection based on what was selected.
 DropDownList1.SelectedIndex = SelectedIndx
 End If

Well there are some adjustments you will have to do, this is just an idea I thought while reading your post.


This question has already been answered. Start a new discussion instead.