Avatar of radhakrishan
radhakrishan
 asked on

Drop Down problem in ASP.Net

Hi,

Im adding items to drop down thru this code. Works Fine.

        cboDay.Items.Clear()

        ldStartDate = DateTime.Parse(cboWEDate.SelectedValue)
        ldStartDate = DateAdd(DateInterval.Day, -6, ldStartDate)

        While ldStartDate <= cboWEDate.SelectedValue
            cboDay.Items.Add(New ListItem(ldStartDate, ldStartDate))
            ldStartDate = DateAdd(DateInterval.Day, 1, ldStartDate)
            Continue While
        End While

But how can I set a DEFAULT value for this ? For instance in this when the loop starts, first ldStartDate would be selected when you go to the page.

ASP.NET

Avatar of undefined
Last Comment
GaryFrancisLond

8/22/2022 - Mon
GaryFrancisLond

You could write the values in reverse order. This can be accomplished by doing this:

cdboDay.Items.Clear()
ldStartDate = DateTime.Parse(cboWEDate.SelectedValue)
Dim i As Int

For i = 1 To 6
  cboDay.Items.Add(New ListItem(ldStartDate, ldStartDate))
  ldStartDate = DateAdd(DateInterval.Day, -1, ldStartDate)
Next i

This will put the date value in cboWEDate.SelectedValue at the bottom of the list and then the 6 previous days above this. Example:

If cboWEDate.SelectedValue was 12/01/2006 you wouold get the following items in the drop down list:

12/01/2006 - Selected by default
11/01/2006
10/01/2006
9/01/2006
8/01/2006
7/01/2006
6/01/2006


An alternative method would be to to call cboDay.SelectedIndex = 6 after your while loop in your code.

Gary Francis
Software Developer
http://www.ssil.co.uk
nauman_ahmed

After the while loop add following:

if cboDay.Items.Count > 0 Then
  cboDay.SelectedIndex = 0
end if

Please make sure that the databinding code is in Page.IsPostBack check i.e.

if not Page.IsPostBack Then
cboDay.Items.Clear()

        ldStartDate = DateTime.Parse(cboWEDate.SelectedValue)
        ldStartDate = DateAdd(DateInterval.Day, -6, ldStartDate)

        While ldStartDate <= cboWEDate.SelectedValue
            cboDay.Items.Add(New ListItem(ldStartDate, ldStartDate))
            ldStartDate = DateAdd(DateInterval.Day, 1, ldStartDate)
            Continue While
        End While
        if cboDay.Items.Count > 0 Then
  cboDay.SelectedIndex = 0
end if

End if

--Nauman.
GaryFrancisLond

Please note I meant 0 in my previosu post not 6.
Your help has saved me hundreds of hours of internet surfing.
fblack61
radhakrishan

ASKER
I have got a WEDate drop down on the page also.

So, when user select WEDate drop down, it fills the DAY dropdown based on WEDate.

So I can never put this in "if not Page.IsPostBack Then".

Really I want this two things, which im not able to solve:
1. when the person goes first time, cboDay.SelectedIndex should be set to 0 (thats clear).

2. thats where the problem is :
When user select any date from "Day DropDown", it fetches the data from database. But when it does that, it sets the "Day" to INDEX 0 or the first date in the drop down. Its not remembering which was the last date selected

Can u pls help.
GaryFrancisLond

Where are you calling your above code from? The Page_Load event?

Gary
radhakrishan

ASKER
Pls see code below and advise:

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

        If Not Page.IsPostBack Then
            cboWEDate.DataBind()
        End If

        cboDay.Items.Clear()
        ldStartDate = DateTime.Parse(cboWEDate.SelectedValue)
        ldStartDate = DateAdd(DateInterval.Day, -6, ldStartDate)
        While ldStartDate <= cboWEDate.SelectedValue
            cboDay.Items.Add(New ListItem(ldStartDate, ldStartDate))
            ldStartDate = DateAdd(DateInterval.Day, 1, ldStartDate)
            Continue While
        End While

        FetchData()
    End Sub

    Protected Sub cboDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDay.SelectedIndexChanged
        FetchData()
    End Sub

    Protected Sub cboWEDate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboWEDate.SelectedIndexChanged
        FetchData()
    End Sub
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pauljk1619

It is because you are not using the "if not ispostback".

Everytime your page loads, it will redo all the dropdowns and lose any selected value.  

Where you want to redo your DAY dropdown is in the Selected Index event of teh WEDate.  
pauljk1619

You probably only want to FetchData for the combo box that changes on the SelectedIndexChanged... not both of them.
GaryFrancisLond

I would suggest that you move the code that populates the day drop down list in the SelectedIndexChanged event of the WEDate drop down list. This way it will not re-execute that code when the page posts back.

Sorry, I assumed you had done it like this already.

Regards,

Gary Francis
Software Developer
http://www.ssil.co.uk
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
pauljk1619

If you change the data in the drop down, even if it's the same data, you will lose the selectedvalue.
radhakrishan

ASKER
 Protected Sub cboDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDay.SelectedIndexChanged
        FetchData()
    End Sub


Can u tell why this won't fire when I select another Date from the Day drop down box.

Regards
GaryFrancisLond

You need to have the AutoPostBack property of the cboDay control set to True.

Regards,

Gary Francis
Software Developer
http://www.ssil.co.uk
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pauljk1619

When are you changing the Day?   After the event for SelectedIndexChanged is fired?   If so, it probably won't fire if you've refetched all the data for it.
radhakrishan

ASKER
When I set the AutoPostBack  to true, then even page reloads the top date in the day dropdown is selected.

Small problem, but im not able to solve it ?

can anobody write small piece of code for me which give this functionality ?

ASKER CERTIFIED SOLUTION
GaryFrancisLond

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question