Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can't I use inline code in asp.net?

Status
Not open for further replies.

timfoster

Programmer
Dec 19, 2002
110
GB
I have the following code in my page:

<asp:dropdownlist id="ddlCardExpYear" runat="server" class="p_smallest">
<% Dim x as Integer
For x = Year(Date()) To Year(Date()) + 10
%>
<asp:ListItem Value="<%=x%>"><%=x%></asp:ListItem>
</asp:dropdownlist>

When I try to run it I get the following error:

Parser Error Message: Code blocks are not supported in this context.


Can anyone tell me why? In classic asp, the above code is valid. How do I do the same in .net? Obviously I want the credit card year and month to be generated rather than hard coded (month maybe wouldn't be a problem, but years will change!)


 
The code behind has taken away the necessity of using inline code. Populate your DropDownList in the codebehind or write a code block on your aspx page if you dont want to use a code behind file.
 
The inline coding can sometimes cause problems with the current .NET way of doing things. It will take some getting used to, but with time and practice you will get it.

This is how I got the results you want with ASP.NET programming (using VB.NET).
You will have to put the code in your code behind page, and add some code to the Page_Load event.
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   If Not IsPostBack Then
      Dim x As Integer
      For x = 0 To 10
         ddlCardExpYear.Items.Add(Year(Date.Now.AddYears(x)).ToString)
      Next x
   End If
End Sub

Hope this helps you on your way.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top