autopopulate a dropdown list

  • gophw1
  • Newbie
  • Newbie
  • gophw1
  • Posts: 12

Post 3+ Months Ago

So I am picking up where someone left off on a site, originally created with Visual Studio
I am attempting to learn .asp on the fly!
the site has a fill in form section that has simple text boxes and some I want to change to drop down menus.
One is for the yr of a vehicle so I need the current yr back to say 1980, i assume there is a way to automate this, one so it stays current each year without editing the code and 2 so I don't have a long list of "list items" in the code.

I'm looking online but would welcome some help from those more knowledgeable, it's probably simple and once I see it it will make sense.

Thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

NOTE:: I by all means am no ASP coder I could write this in PHP in no time. I did a quick search and came out with this.

// Add this to the page
Code: [ Select ]
Year <asp:DropDownList ID="YearShow" runat="server" AutoPostBack="True">    
    </asp:DropDownList>
  1. Year <asp:DropDownList ID="YearShow" runat="server" AutoPostBack="True">    
  2.     </asp:DropDownList>


// Backend Code .. I believe in C#
Code: [ Select ]
if(!IsPostBack) {
  int year = System.DateTime.Now.Year;
  for (int intCount = 1900; intCount <= year; intCount++) {
    YearShow.Items.Add(intCount.ToString());
  }
}
  1. if(!IsPostBack) {
  2.   int year = System.DateTime.Now.Year;
  3.   for (int intCount = 1900; intCount <= year; intCount++) {
  4.     YearShow.Items.Add(intCount.ToString());
  5.   }
  6. }


OR in VB?
Code: [ Select ]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  If Not IsPostBack Then
    Dim year As Integer = System.DateTime.Now.Year
    For intCount As Integer = 1900 To year
      YearShow.Items.Add(intCount.ToString())
    Next
  End If
End Sub
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.   If Not IsPostBack Then
  3.     Dim year As Integer = System.DateTime.Now.Year
  4.     For intCount As Integer = 1900 To year
  5.       YearShow.Items.Add(intCount.ToString())
  6.     Next
  7.   End If
  8. End Sub


I have absolutely no way to test this out so I don't know if it works.
  • gophw1
  • Newbie
  • Newbie
  • gophw1
  • Posts: 12

Post 3+ Months Ago

Looks close to what I found searching around also, going to try to test both out later.
Thanks for the help!
  • gophw1
  • Newbie
  • Newbie
  • gophw1
  • Posts: 12

Post 3+ Months Ago

Works like a champ, after I figured out the back end of it!
It's tough teaching an old dog new tricks!

Thanks for the help!

Actually, how do I switch it so the current year shows instead of the other?
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

I'm not sure what you mean by other? I don't have a way of testing asp but if it a factor of reversing the for loop it could be something like this

Again I don't know it this will work or not or if this is what you were looking for

C#
Code: [ Select ]
if(!IsPostBack) {
 int year = System.DateTime.Now.Year;
 for (int intCount = year; intCount >= 1900; intCount--) {
  YearShow.Items.Add(intCount.ToString());
 }
}
  1. if(!IsPostBack) {
  2.  int year = System.DateTime.Now.Year;
  3.  for (int intCount = year; intCount >= 1900; intCount--) {
  4.   YearShow.Items.Add(intCount.ToString());
  5.  }
  6. }


VB
Code: [ Select ]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 If Not IsPostBack Then
  Dim year As Integer = System.DateTime.Now.Year
  For year To 1900 Step -1
   YearShow.Items.Add(intCount.ToString())
  Next
 End If
End Sub
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  If Not IsPostBack Then
  3.   Dim year As Integer = System.DateTime.Now.Year
  4.   For year To 1900 Step -1
  5.    YearShow.Items.Add(intCount.ToString())
  6.   Next
  7.  End If
  8. End Sub
  • nakia
  • Born
  • Born
  • nakia
  • Posts: 2

Post 3+ Months Ago

Can you share the php version here scott ?
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

Here you go
PHP Code: [ Select ]
<?php
 
// Setup the array to hold the options
$dates_options = array();
 
// Loop from 1900 to the current year
for($i=1900; $i<=date('Y'); $i++) {
   
   // Build the date options and add to the array
   $dates_options[] = '<option value="'. $i .'">'. $i .'</option>';
   
}
 
// There are two ways of going from the current year to 1900. one if by using the above for loop
// and reversing the array and the other is by using the for loop below
$dates_options_rev1 = implode("\r\n\t", array_reverse($dates_options));
 
// Turn into a string
$dates_options = implode("\r\n\t", $dates_options);
 
// Setup the array to hold the options
$dates_options_rev2 = array();
 
// Loop from the current year to 1900
for($i=date('Y'); $i>=1900; $i--) {
 
   // Build the date options and add to the array
   $dates_options_rev2[] = '<option value="'. $i .'">'. $i .'</option>';
   
}
 
// Turn into a string
$dates_options_rev2 = implode("\r\n\t", $dates_options_rev2);
 
?>
 
1900 to current year<br />
<select name="dates" id="dates">
   <?php echo $dates_options; ?>
   
</select>
<br />
<br />
current year to 1900 rev1<br />
<select name="dates" id="dates">
   <?php echo $dates_options_rev1; ?>
   
</select>
<br />
<br />
current year to 1900 rev2<br />
<select name="dates" id="dates">
   <?php echo $dates_options_rev2; ?>
   
</select>
 
  1. <?php
  2.  
  3. // Setup the array to hold the options
  4. $dates_options = array();
  5.  
  6. // Loop from 1900 to the current year
  7. for($i=1900; $i<=date('Y'); $i++) {
  8.    
  9.    // Build the date options and add to the array
  10.    $dates_options[] = '<option value="'. $i .'">'. $i .'</option>';
  11.    
  12. }
  13.  
  14. // There are two ways of going from the current year to 1900. one if by using the above for loop
  15. // and reversing the array and the other is by using the for loop below
  16. $dates_options_rev1 = implode("\r\n\t", array_reverse($dates_options));
  17.  
  18. // Turn into a string
  19. $dates_options = implode("\r\n\t", $dates_options);
  20.  
  21. // Setup the array to hold the options
  22. $dates_options_rev2 = array();
  23.  
  24. // Loop from the current year to 1900
  25. for($i=date('Y'); $i>=1900; $i--) {
  26.  
  27.    // Build the date options and add to the array
  28.    $dates_options_rev2[] = '<option value="'. $i .'">'. $i .'</option>';
  29.    
  30. }
  31.  
  32. // Turn into a string
  33. $dates_options_rev2 = implode("\r\n\t", $dates_options_rev2);
  34.  
  35. ?>
  36.  
  37. 1900 to current year<br />
  38. <select name="dates" id="dates">
  39.    <?php echo $dates_options; ?>
  40.    
  41. </select>
  42. <br />
  43. <br />
  44. current year to 1900 rev1<br />
  45. <select name="dates" id="dates">
  46.    <?php echo $dates_options_rev1; ?>
  47.    
  48. </select>
  49. <br />
  50. <br />
  51. current year to 1900 rev2<br />
  52. <select name="dates" id="dates">
  53.    <?php echo $dates_options_rev2; ?>
  54.    
  55. </select>
  56.  
  • nakia
  • Born
  • Born
  • nakia
  • Posts: 2

Post 3+ Months Ago

Thanks scott

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 87 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.