PDA

View Full Version : Static Dropdownlist not selecting correct Value


Punkcrib
05-07-2008, 10:18 PM
I have two static DDL's (meaning they are not data bound, but rather have their items entered manually at design time). These DDL's correspond to Months and Years. The Month's DDL has Jan-Dec, with values of 1-12. The Year's DDL has 2000-2015, with values of 2000-2015.

This is my .aspx page's body code:
<form id="form1" runat="server">
<div style="text-align: center">
<asp:Label ID="Label1" runat="server" Text="Select a month and year to view: "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>&nbsp;
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>2000</asp:ListItem>
<asp:ListItem>2001</asp:ListItem>
<asp:ListItem>2002</asp:ListItem>
<asp:ListItem>2003</asp:ListItem>
<asp:ListItem>2004</asp:ListItem>
<asp:ListItem>2005</asp:ListItem>
<asp:ListItem>2006</asp:ListItem>
<asp:ListItem>2007</asp:ListItem>
<asp:ListItem>2008</asp:ListItem>
<asp:ListItem>2009</asp:ListItem>
<asp:ListItem>2010</asp:ListItem>
<asp:ListItem>2011</asp:ListItem>
<asp:ListItem>2012</asp:ListItem>
<asp:ListItem>2013</asp:ListItem>
<asp:ListItem>2014</asp:ListItem>
<asp:ListItem>2015</asp:ListItem>
<asp:ListItem>2016</asp:ListItem>
</asp:DropDownList>&nbsp;
<asp:Button ID="Button1" runat="server" Text="Display" />
<br />
<br />

<%ReadData()%>
</div></form>

That is all that is coded on the page. The "ReadData()" method creates an event calendar that writes out a table w/ event info from a database. This is all working fine, except for the following:

In my ReadData() method I query the month and year. If no month or year was specified, I get the current month and year (using Month(Date.Now()), etc) and set those values in "intMonth" and "intYear". This works fine as it should. However, right after this I try to change the selected items of the DDL's to reflect intMonth and intYear by doing the following:

Me.DropDownList1.SelectedValue = intMonth
Me.DropDownList2.SelectedValue = intYear

However, when the page displays it always defaults to selecting the 1st items in each DDL. I even created a button on the page that when clicked will execute the two statements above and that works fine. But it is not working when the page loads like it should.

Any ideas? I'm stumped.

Freon22
05-09-2008, 06:16 PM
So did you get it working?

If not look into PostBack, because I think this is what your problem maybe.

See video.
http://asp.net/learn/videos/video-12.aspx

chump2877
05-13-2008, 01:07 AM
I'm not sure why you are calling ReadData() directly from your .aspx file instead of attaching the function call to a Page or Control event (in your code-behind .cs file)?

It likely does have something to do with the page's postback, and you confusing the order of events in the page's life cycle (http://msdn.microsoft.com/en-us/library/ms178472.aspx).

From the code you have posted though, it's almost impossible to tell what the real cause is because you've posted your markup -- but almost none of the underlying C#.

Punkcrib
05-20-2008, 05:23 PM
Ok, forget about there being any additional code behind. I've trimmed down the page to only have the 2 dropdownlists, and the .vb file to only have the ReadData() method that gets called in the page's body (as stated above).

The only code-behind is the ReadData method which just gets the current date from the server and sets the dropdowns' selected value - this is posted above (where I am trying to change the defaulted/selected values of the dropdown). I understand postback and have ensured that postback is turned off for these dropdownlists.

So to go over this again, there are 2 dropdowns on the aspx page with static items. when the page is loaded (or when the ReadData() is called from the aspx page), ReadData() (in the .vb file) gets the current date, determines the month & year (as two integers), and then sets the 2 dropdownlists selected value to the month and year, respectively.

The dropdown lists are not properly selected when they are displayed though.

Ideas?

majidkorai
05-28-2010, 09:42 AM
Well this can be determined only by looking at the code? How do u select the dates in code behind from your ReadData() method.... ?

Please post your code here...

Well i am having a little similar problem,
I am having four dropdownlist control on my Usercontrol.
1. dropdownlistMonthFrom (this is similar to above and have a list of months along with its corresponding value. e.g. for jan - 1, feb - 2 , and so .. )
2. dropdownlistYearFrom (this holds a range of years, along with values. 2001 - 2001, 2002 - 2002 and so )
3. dropdownlistMonthTo (similiar as above)
4. dropdownlistYearTo ( similiar as above)
5. A button control, that submits the selected values.

what i am doing in code behind, parse the selected values into integer type and filter out some of my files on the basis of these values.

Now the problem is that all works fine on my localhost. But when i upload the code online, my filter stops working.

An example: I select (Jan 2002 to Feb 2002). Now i have got files for the jan as well as feb. Filter should work and display both the files. and it is doing so on my local machine. But when i upload my code online, its messing. No file within this range.. Although it does something like this when i change the year value of dropdownFromYear to some back year (e.g. 2001) then it picks all the files from directory and displays them.

Is there any one who can figure this out ????