?
Solved

Dropdownlist not changing to selected item

Posted on 2009-04-30
14
Medium Priority
?
207 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Cahl
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24273835
Sorry, i couldnt understand what u want to do
0
 

Author Comment

by:Cahl
ID: 24273932
Sorry about that. I want the selected item in the dropdown to be the current Month or Year,
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24273996
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cahl
ID: 24274285
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.
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 664 total points
ID: 24274530
When and where are the GSWA_LoadMonths() and GSWA_LoadYears() procedures called?

0
 
LVL 9

Assisted Solution

by:AsishRaj
AsishRaj earned 664 total points
ID: 24274687
Call this two  after you have populated you DDL - that is after GSWA_LoadMonths()  and GSWA_LoadYears()

ddlGSWAYear.SelectedValue = tnow.Year.ToString
  ddlGSWAMonth.SelectedValue = tnow.Month.ToString()


0
 

Author Comment

by:Cahl
ID: 24274957
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

0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24275062
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

0
 
LVL 9

Accepted Solution

by:
CyberLex earned 672 total points
ID: 24276099
Greetings :)

You are filling the months with Monthname which returns a string (eg. January).

but then you try to find the current month for selection with "Now.Month" which returns an integer (month number).

you will need to do the selection with "monthname(now.month)"

Change:
genDDL.Items.FindByValue(Now.Month).Selected = True

to:

genDLL.items.FindByValue(MonthName(Now.Month)).Selected = True

sidenote
VB will convert the integer conversion automatically to a string, that's why you dont get an exception. But you'll never find an integer (number) in your monthname :)

cheers
CyberLex
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24276300
Greeting CyberLex


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

Author Comment

by:Cahl
ID: 24280861
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

0
 
LVL 9

Expert Comment

by:CyberLex
ID: 24281596
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
0
 

Author Comment

by:Cahl
ID: 24282182
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.
0
 
LVL 9

Expert Comment

by:CyberLex
ID: 24282534
:=) good to hear & have fun
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question