Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 27th, 2012, 05:16 PM
Authorized User
Join Date: Jun 2003
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Can anyone please help!!! Problem with the IF statement...

Hi All,

I am new to the asp.net development. I know classic asp very well but I am having some difficulty moving to the asp.net

Please find below a problem I am encountering in one of my master pages.

Dim ArticleType AsString
ArticleType = DataBinder.Eval(Container.DataItem, "eventnews_type")

If (ArticleType = "event") Then
Response.Write("Upcoming Event:")

<asp:LabelID="eventnews_dateLabel"runat="server"Text='<%# Eval("eventnews_date") %>'/>
<b><ahref=newszoom.aspx?article=<%# Eval("eventnews_id") %>><asp:LabelID="eventevent_titleLabel"runat="server"Text='<%# Eval("eventevent_title") %>'/></a></b>

The problem is in the if statement. If I remove the if statement the website compiles without any problem. All I want is to simply write UPCOMING EVENT if the value from the database eventnews_type is = to event. I am receiving error 'Container' is not declared. It may be inaccessible due to its protection level.

I tried various ways but can't solve this problem. I know I am missing something simple.

Can you please help me.

Thanks in advance.

Old September 28th, 2012, 09:35 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi there,

You're mixing classic ASP techniques with ASP.NET which won't work. Your controls are built during the data binding process, and then the combined HTML for the control is injected in the page. Response.Write on the other hand writes directly to the response. So, even if this code would run, the result from Response.Write would show up at a completely different location (at the top of the page).

Instead, you need to use data binding syntax. You can use a Label or a Literal and then have code like this:

  <asp:Literal runat="server" Text='<%# DetermineType(Eval("eventnews_type")) %>'/>
  <asp:LabelID="eventnews_dateLabel"runat="server"Text='<%# Eval("eventnews_date") %>'/>
  <b><ahref=newszoom.aspx?article=<%# Eval("eventnews_id") %>><asp:LabelID="eventevent_titleLabel"runat="server"Text='<%# Eval("eventevent_title") %>'/></a></b>
Then add the following method to the code behind:

Protected Function DetermineType(newsType As Object) As String
  If newsType.ToString() = "event" Then
    Return "Upcoming Event:"
    Return ""
  End If
End Function
A few other comments:

1. If you post code, can you please paste it in Notepad first to remove color coding and then use the forum editor's Code toolbar button to wrap your code in code tags. Otherwise, this forum messes up the code.

2. The DataList control is considered outdated and deprecated. Instead, look at the ListView which enables you to do the same stuff, but with more flexibility.

3. Consider getting a copy of my book Beginning ASP.NET 4 in C# and VB (or wait for the 4.5 version that should be out soon). I discuss stuff like this in great detail in that book.


Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
ivanv (October 6th, 2012)
Old October 2nd, 2012, 06:07 AM
Authorized User
Join Date: Jun 2003
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thank You!!

Thank Imar,

You solved my problem completely. I will for sure buy the book when the new one is released.

Thanks and Regards


Similar Threads
Thread Thread Starter Forum Replies Last Post
If statement problem chris1012 ASP.NET 3.5 Basics 3 September 9th, 2008 05:52 PM
problem with if...or...then statement lightmaker Classic ASP Basics 7 June 9th, 2008 06:49 PM
Where statement problem lryckman Access VBA 4 June 29th, 2007 08:46 AM
Problem with FOR Statement bleutiger Classic ASP Databases 3 February 23rd, 2005 11:22 PM
problem in prepared statement abhit_kumar JSP Basics 1 November 25th, 2004 04:22 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.