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.S electedVal ue)
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.
Im adding items to drop down thru this code. Works Fine.
cboDay.Items.Clear()
ldStartDate = DateTime.Parse(cboWEDate.S
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.
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.S electedVal ue)
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.
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.S
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.
Please note I meant 0 in my previosu post not 6.
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.
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.
Where are you calling your above code from? The Page_Load event?
Gary
Gary
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.S electedVal ue)
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_SelectedIndexChange d(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDay.SelectedIndexChange d
FetchData()
End Sub
Protected Sub cboWEDate_SelectedIndexCha nged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboWEDate.SelectedIndexCha nged
FetchData()
End Sub
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.S
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_SelectedIndexChange
FetchData()
End Sub
Protected Sub cboWEDate_SelectedIndexCha
FetchData()
End Sub
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.
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.
You probably only want to FetchData for the combo box that changes on the SelectedIndexChanged... not both of them.
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
Sorry, I assumed you had done it like this already.
Regards,
Gary Francis
Software Developer
http://www.ssil.co.uk
If you change the data in the drop down, even if it's the same data, you will lose the selectedvalue.
ASKER
Protected Sub cboDay_SelectedIndexChange d(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDay.SelectedIndexChange d
FetchData()
End Sub
Can u tell why this won't fire when I select another Date from the Day drop down box.
Regards
FetchData()
End Sub
Can u tell why this won't fire when I select another Date from the Day drop down box.
Regards
You need to have the AutoPostBack property of the cboDay control set to True.
Regards,
Gary Francis
Software Developer
http://www.ssil.co.uk
Regards,
Gary Francis
Software Developer
http://www.ssil.co.uk
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.
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 ?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cdboDay.Items.Clear()
ldStartDate = DateTime.Parse(cboWEDate.S
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