views:

106

answers:

6

I'm just starting to do web pages in ASP and all of the code just feels... messy.

<% if (new Random().NextDouble() < 0.5) { %>
    <asp:Image ID="image" runat="server" ImageUrl="~/1.jpg" />
<% } else { %>
    <asp:Image ID="image" runat="server" ImageUrl="~/2.jpg" />
<% } %>

Currently, I have a very basic page that is light on content, but eventually I am going to have to add logic and more display elements to this. So I come to you, SO.

First, how would I clean up this small segment of code? Second, what do I need to know going forward?

Edit: The asp:Image tags were generated from VS2008, and then I added the if blocks based on what I found online. I have plenty of experience with C#, but absolutely none with ASP, so if there's more subtleties about this code that I'm not grasping, by all means enlighten!

A: 

ASP.NET WebForms is a bit messy. That was one of its drawbacks, along with ViewState and the rather convoluted eventing model. If you wish to use a cleaner platform for developing web sites, I recommend ASP.NET MVC.

jrista
though MVC is the bees knees, this isn't really an answer to the question.
rockinthesixstring
@rock: It was not an answer to some parts of the question, however it was to other parts. He stated that ASP.NET WebForms feels messy, and MVC is essentially the answer to that.
jrista
I suppose this is true. (fyi, I didn't downvote)
rockinthesixstring
To be fair, the code he used to illustrate the "messiness" of ASP.NET is pretty much how you would write it in ASP.NET MVC (except you'd use HTML img tags rather than an asp:Image). So I'm not sure if switching to ASP.NET MVC would actually un-messify this particular block of code.
Kevin Pang
not entirely true @Kevin. If I'm just switching a single image I approach it in one of two ways in MVC (neither of which are the same as the OP's code). I either Switch it in my ViewModel and send it down the line, or I switch it with an inline if in the markup so that I can still maintain a single line of code instead of an big if block.
rockinthesixstring
@jrista, I +1 to bring balance to the force.
rockinthesixstring
@rock: Thanks. :) @Kevin: Sure, it is possible to use the same exact syntax in MVC, however general practice for MVC is to use simpler syntax, often facilitated by HTML Helpers. MVC also does not have any ASP.NET controls...its just plain old html markup (POHM). You also have the option of using an alternative view engine, such as NVelocity, or if you are a hard-core minimalist, a StringTemplate engine, to have considerably cleaner views than would ever be possible with WebForms.
jrista
I'm not debating whether ASP.NET MVC is cleaner than ASP.NET Webforms. I'm just saying that if this particular block of code is what's bothering the OP, then switching to ASP.NET MVC isn't going to make it any cleaner. The suggestion to either put the logic in the ViewModel or use an inline if in the markup are both possible with ASP.NET Webforms (obviously inline is available and the logic can go in the codebehind instead of ViewModel). Overall, I think ASP.NET MVC will result in cleaner code, just not in this particular example.
Kevin Pang
+2  A: 

what about an inline if statement. This allows you to manipulate the imageUrl in 1 line instead of 5

    <asp:Image ID="image" runat="server" ImageUrl='<%= (new Random().NextDouble() < 0.5) ? "~/1.jpg" : "~/2.jpg" %>' />

As for your second question.. There's a lot to .NET and you need to know a decent chunk of it going forward. Look into some design patterns (MVC is my favorite)

rockinthesixstring
+8  A: 

In the markup:

<asp:Image ID="image" runat="server" />

In the code-behind:

...
if (new Random().NextDouble() < 0.5)
{
    image.ImageUrl = "~/1.jpg";
}
else
{
    image.ImageUrl = "~/2.jpg";
}
arootbeer
This is how I do my WebForms as well, however once I moved to MVC, I learned really quickly that avoiding code in the markup is not really possible. It's good to know both options.
rockinthesixstring
It's definitely important to know both options, but one of the primary purposes of ASP.Net was to get the code out of the markup as had been the requirement with ASP. The question is about style in ASP.Net specifically.
arootbeer
right, but the OP doesn't specify ASP.NET WebForms or ASP.NET MVC (both are ASP.NET). Though he's eluding to WebForms with the `runat="server"` declaration.
rockinthesixstring
Clarified in the tags - currently running on webforms.
MikeD
A: 

Better still, try to have as little code as possible in the markup of the page and set the ImageUrl of image in the page load event or another method.

Simon Hazelton
what happens if he moves to MVC?
rockinthesixstring
Then the controller should decide which image to display and the view should be something like <img src="<%= Model.ImageUrl%>" alt="foo" />
Simon Hazelton
I wouldn't do it in the controller either. If I were switching from the back end I'd actually do it in a ViewModel.
rockinthesixstring
+3  A: 

In your specific case,

<asp:Image ID="image" runat="server" 
           ImageUrl='~/<%= new Random().Next(1,3) %>.jpg' />

But in general, choosing to do things in code behind or in the markup is a choice you have to make. You may like Razor, a new way of doing in markup code. I haven't tried it yet myself, but it looks a bit cleaner.

BioBuckyBall
A: 

So every refresh/postback might result with a different image? Sounds too random for me.

Ron Klein