Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:14:24+00:00 2026-05-27T22:14:24+00:00

I wonder if is possible to add DropDown item in the Page instead of

  • 0

I wonder if is possible to add DropDown item in the Page instead of Code-behind?

So say I have a DropDown box I want to show year 1900 ~ current year

<asp:DropDownList ID="ddlYear" runat="server" CssClass="text">
    <% 
        int currentYear = DateTime.Now.Year;
        for (int i = 1900; i < currentYear; i++)
        {
    %>
        <asp:ListItem><%=i %></asp:ListItem>
    <%
        }
    %>
</asp:DropDownList>

Is it possible to do something like that?

Because when I try to do like above it gives me:

Warning 1 C:\SandBox\MyWebApplication1\MyWebApplication1\Default.aspx:
ASP.NET runtime error: Code blocks are not supported in this
context. C:\SandBox\MyWebApplication1\MyWebApplication1\Default.aspx 35 1 MyWebApplication1

Edit: I also tried:

<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>

<% 
    int currentYear = DateTime.Now.Year;

    if (currentYear <= 1933)
    {
        currentYear = 2012;
    }
    for (int i = 1933; i < currentYear; i++)
    {
        DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
    }
%>

But the DropDownList doesn’t have items when display…

=====
Edit: As Splash-X suggested, now it rendered, but all items gone after postback….

<form id="form1" runat="server">
<div>
<% 
    int currentYear = DateTime.Now.Year;
    if (!Page.IsPostBack)
    {
        if (currentYear <= 1933)
        {
            currentYear = 2012;
        }
        DropDownList1.Items.Clear();
        for (int i = 1933; i < currentYear; i++)
        {
            DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }
%>
<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>



    <input id="Button1" type="button" value="button" onclick="MyFunction();"/>
    <div id="MyDiv">
    </div>
    <asp:Button ID="Button2" runat="server" Text="Button" />
</div>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:14:25+00:00Added an answer on May 27, 2026 at 10:14 pm

    I had an instance where we had a project written in ASP.NET 2.0 and we no longer had the source code to make changes. We needed to modify the states that were being displayed in a dropdown. The old code had US States and Canadian Provinces and we needed to display States only. The dropdown was bound to data source in the code behind so we clear the items and then add all of the items.

    <%
    if(!IsPostBack)
    {
        st.Items.Clear();
        st.Items.Add(new ListItem("State", "")); //Add an empty item at the top for default value        
        string usStates = "AA,AE,AK,AL,AP,AR,AS,AZ,CA,CO,CT,DC,DE,FL,FM,GA,GU,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MH,MI,MN,MO,MP,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,PR,PW,RI,SC,SD,TN,TX,UT,VA,VI,VT,WA,WI,WV,WY";
        foreach (string state in usStates.Split(','))
        {
            st.Items.Add(new ListItem(state, state));
        }
    }
    %>
    <select name="st" class="col" id="st" alt="select" runat="server">
    

    The difference is I’m doing my modifications BEFORE the control and the reason I had to do this is any script that comes after the control is changing the dropdown after it is rendered out to the page.

    Try moving your code above the dropdown list. Our code checks to see if there was a post back so we could preserve any value that was previously entered. There is no point reloading the list on every page load, just the first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if it's possible to add an Event Listener to a MovieClip that
I wonder if it is possible to show OSM ( Open Street Maps )
I wonder if it possible to not have jython automagicaly transform java objects to
I wonder if it's possible to add a button directly into a navigation controller
I wonder if it's possible to have several div elements with different id that
I'm working in a Delphi IDE expert and I wonder if it's possible add
I wonder if it's possible to add a line to the design in a
I wonder if it's possible to add an object from one list to another
I wonder if it is possible to add or attach or extend the asp.net
I wonder if this is possible. I have a existing WCF service, being consumed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.