Solved

asp.net vb display current year in dropdownlist

Posted on 2010-09-18
7
664 Views
Last Modified: 2012-05-10
How would i modify this dropdownlist item to display the current year as both the value and the display?

<asp:listitem value="current year here" selected="true">and current ear here</asp:listitem>

Thanks
0
Comment
Question by:elliottbenzle
7 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 33708786
Could you set the value in the code behind it?

asp like this  <asp:DropDownList ID="DropDownList1" runat="server" Height="59px"></asp:DropDownList>

then in code like this

 DropDownList1.Items.Add(Year(Now()));
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33708802
Might need to tostring the year(now) bit if it complains

in order to selected the display value you can use selected value

eg see this example (in c#) where I added 3 entries in my dropdownlist. I set the selected value by using the value I want

        DropDownList1.Items.Add("2009");
        DropDownList1.Items.Add(dt.Year.ToString());
        DropDownList1.Items.Add("2011");
        DropDownList1.SelectedValue = dt.Year.ToString();
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33708816
Yes, I would do it with ToString() and remember you can do this inline.

ddlYear.Items.Add(new ListItem(DateTime.Today.ToString("yyyy"), DateTime.Today.ToString("yyyy"), true))
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33708819
That way you also set the enabled to true in one go.
0
 
LVL 5

Expert Comment

by:FredTang
ID: 33709900
               dim Item_year as Listitem
                lItem_year = new ListItem();
                lItem_year.Text = Year(Now()).toString();
                lItem_year.Value = Year(Now()).toString();
                this.DropDownList1.Items.Add(lItem_year);

hopes this code could help you.
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33710188
Thanks
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33710207
No worries :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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