Link to home
Start Free TrialLog in
Avatar of Cahl
CahlFlag for United States of America

asked on

Dropdownlist not changing to selected item

I have 2 dropdown lists, one for months, 1 for years. after loading them, I want the current month/year to be selected. The lists are being populated but the selected item is the first one, not the current month or year.
Sub GSWA_LoadMonths()
        Dim i As Integer
        ddlGSWAMonth.Items.Clear()
        For i = 1 To 12
            ddlGSWAMonth.Items.Add(New ListItem(MonthName(i), i))
        Next
        ddlGSWAMonth.Items.FindByValue(Now.Month).Selected = True
    End Sub
    Sub GSWA_LoadYears()
        Dim i As Integer, iYear As Integer
        With ddlGSWAYear.Items
            .Clear()
            For i = -3 To 0
                iYear = Now.AddYears(i).Year
                .Add(New ListItem(iYear, iYear))
            Next
            .Add(New ListItem(Now.AddYears(1).Year, Now.AddYears(1).Year))
        End With
        i = Now.Year
        'ddlGSWAYear.Items.FindByValue(i).Selected = True
        ddlGSWAYear.SelectedIndex = ddlGSWAYear.Items.IndexOf(ddlGSWAYear.Items.FindByValue(i))
    End Sub

Open in new window

Avatar of AsishRaj
AsishRaj
Flag of Fiji image

Sorry, i couldnt understand what u want to do
Avatar of Cahl

ASKER

Sorry about that. I want the selected item in the dropdown to be the current Month or Year,
use this in Page_Load funtion after you have called GSWA_LoadMonths()


If Not IsPostBack Then
 
  Dim tnow As DateTime
  tnow = DateTime.Now
 
  ddlGSWAYear.SelectedValue = tnow.Year.ToString
  ddlGSWAMonth.SelectedValue = tnow.Month.ToString()
 
End IF

Open in new window

Avatar of Cahl

ASKER

I put that in and no change. The procedures aren't called until the user clicks on the report name to open that particular panel.

Since a user may or may not run the particular report, I thought I'd wait to load them until they were needed.
SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cahl

ASKER

Since I posted the question, I've made a change to the code so I can use it to populate two different sets of dropdownlists The first set is on pnlGSWA, the second is on pnlMonthYear.

When the user clicks on a item in the menu, the panel.visible = true and the dropdownlists are loaded. It seems to be working on pnlMonthYear (added this after posting this question) but not on pnlGSWA. I've included the code for loading the panels.
Sub ddl_LoadMonths(ByVal ctrl As Control)
        Dim genDDL As DropDownList = CType(ctrl, DropDownList)
        Dim i As Integer
        With genDDL.Items
            .Clear()
            For i = 1 To 12
                .Add(New ListItem(MonthName(i), i))
            Next
        End With
        genDDL.Items.FindByValue(Now.Month).Selected = True
    End Sub
    Sub ddl_LoadYears(ByVal ctrl As Control)
        Dim genDDL As DropDownList = CType(ctrl, DropDownList)
        Dim i As Integer, iYear As Integer
        With genDDL.Items
            .Clear()
            For i = -3 To 0
                iYear = Now.AddYears(i).Year
                .Add(New ListItem(iYear, iYear))
            Next
            .Add(New ListItem(Now.AddYears(1).Year, Now.AddYears(1).Year))
        End With
        'i = Now.Year
        'ddlGSWAYear.Items.FindByValue(i).Selected = True
        genDDL.SelectedValue = Now.Year.ToString
    End Sub
 
Protected Sub MenuReports_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles MenuReports.MenuItemClick
        Dim iType As String, iText As String
        iText = MenuReports.SelectedItem.Text
        iType = MenuReports.SelectedItem.Value.ToString.ToLower
        pnlGSWAQualityProgram.Visible = False
        pnlReportsByDates.Visible = False
        Select Case iType
 Case "gswaqualityprogram"
                ' pnlFacts.Visible = False
                pnlGSWAQualityProgram.Visible = True
                ddl_LoadMonths(ddlGSWAMonth)
                ddl_LoadYears(ddlGSWAYear)
                GSWA_LoadRadio()
                ResetTextBoxes(pnlGSWAQualityProgram)
                With lnkGSWAReport
                    .Enabled = False
                    .Visible = False
                End With
                With lnkGSWACancel2
                    .Enabled = False
                    .Visible = False
                End With
            Case "prodqualgraph"
                pnlMonthYear.Visible = True
                ddl_LoadMonths(ddlMonthYear_Month)
                ddl_LoadYears(ddlMonthYear_Year)
 
        End Select

Open in new window

put this line after the case statement


Dim tnow As DateTime
  tnow = DateTime.Now
 
  ddlGSWAYear.SelectedValue = tnow.Year.ToString
  ddlGSWAMonth.SelectedValue = tnow.Month.ToString()

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Greeting CyberLex


yeap i totally missed that. guess just overlooked sorry for that Cahl
Avatar of Cahl

ASKER

Thank you for the sidenote: I was confused as to why your solution should work. As it was, I had to read it a few times to catch on :)

Unfortunately, when I put your solution in, I get "Object reference not set to an instance of an object" on the line of your solution.
Sub ddl_LoadMonths(ByVal ctrl As Control)
        Dim genDDL As DropDownList = CType(ctrl, DropDownList)
        Dim i As Integer
        With genDDL.Items
            .Clear()
            For i = 1 To 12
                .Add(New ListItem(MonthName(i), i))
            Next
        End With
        genDDL.Items.FindByValue(MonthName(Now.Month)).Selected = True
    End Sub

Open in new window

Greetings

probably because genDll.Items.FindByValue(xx) is returning an integer and not an object, so the .select method can't work.

this did work in vb.net here tho

genDDL.SelectedIndex = genDDL.Items.IndexOf(MonthName(Now.Month))

hope this helps,
cheers
Lex
Avatar of Cahl

ASKER

I tried your suggestion and get a blue line with the info "Value type of String cannot be converted to listitem".

After a bit of pondering, I copied a line from DDL_LoadYears() and changed it to reference the month: genDDL.SelectedValue = Now.Month

Here's the strange part. For ddlMonthYear_Month & ddlMonthYear_Year, the current month and year are selected. ddlGSWAMonth and ddlGSWAYear, the selection stays at the first item on the list.

I looked in MenuReports code at the difference between the loading of the panels and discovered something. For the loading of the gswa panel I was calling a procedure to reset the controls on the panel AFTER running the procedures to load the dropdownlists! So the selection was being reset to the first item. Doh!

Thank you for your help in sorting through all this, going through the process helped me figure out what was going on.
:=) good to hear & have fun