My first C# ASP.Net project

Well I’m no C# fundi, or .Net expert, but sometimes you may need to use these technologies which can be quite fun. I recently had to help one of my work colleague out with creating a simple website that should have dynamic link navigation, and a simple way of changing the flash file based on the clicked url.

I have played with C# before in my spare time and created a simple desktop app to store documents. Unfortunately, I did not get to fully complete the app.

Now back to the current project. Here is the asp part of the code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SomeWebsite._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="id_menu">
        <asp:Menu ID="Navigation" runat="server" OnMenuItemClick="Navigation_MenuItemClick" 
            Font-Italic="True" Orientation="Horizontal" BorderStyle="None">
            <StaticSelectedStyle HorizontalPadding="15px" />
            <Items>
                <asp:MenuItem Text="URL1" Value="URL1"></asp:MenuItem>
                <asp:MenuItem Text="URL2" Value="URL2"></asp:MenuItem>
            </Items>
        </asp:Menu>
    </div>
    </form>
    <div id="id_flash_embed" runat="server"></div>                                   
</body>
</html>

Pretty simple layout and navigation. Since we’re using the ASP.Net menu component, it allows us the option to dynamically get the urls from an xml file or sitemap. This makes it very easy for us to create a dynamic url structure.

And here is the C# code that is behind the asp code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SomeWebsite
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Navigation_MenuItemClick(object sender, MenuEventArgs e)
        {
            //Use a dictionary to map my url text and swf file name. Dictionaries are awesome!
            Dictionary<string, string> urlMap = new Dictionary<string, string>();

            //The swf embed string that will be returned.
            string swfResponse = "";

            //The swf embed heredoc string template.
            string embedString = @"<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' WIDTH='1000' HEIGHT='1500' id='myMovieName'>
                                        <PARAM NAME='movie' VALUE='Swfs\find_and_replace_me.swf'> 
                                        <PARAM NAME='quality' VALUE='high'>
                                        <PARAM NAME='bgcolor' VALUE='#FFFFFF'>
                                        <PARAM NAME='play' VALUE='true'>
                                        <PARAM NAME='loop' VALUE='true'>
                                        <PARAM NAME=bgcolor VALUE='#FFFFFF'>
                                        <EMBED src='find_and_replace_me.swf' quality=high bgcolor=#FFFFFF WIDTH='1000' HEIGHT='1500' NAME='myMovieName' ALIGN='' TYPE='application/x-shockwave-flash' play='true' loop='true' PLUGINSPAGE='http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'> </EMBED>
                                   </OBJECT>";

            //Set up all of the url text values and flash file names respectively.
            urlMap.Add("URL1", "swfFile1");
            urlMap.Add("URL2", "swfFile2");

            //Generate the embed flash string based on the link clicked and then add it to the html.
            string replaceVal;
            if (urlMap.TryGetValue(Navigation.SelectedItem.Text, out replaceVal))
            {
                swfResponse = embedString.Replace("find_and_replace_me", replaceVal);
                id_flash_embed.InnerHtml = swfResponse;
            }
            else 
            {
                id_flash_embed.InnerHtml = "<strong>Sorry, the link you clicked has no data associated with it.</strong>";
            }
            
        }
    }
}

As you may notice, I have used a dictionary here; dictionaries are awesome! It cuts down the code quite a bit since there is no need to use if..else statements to test the url and get the correct flash file.

So that was my first ASP.Net app that I did all by myself. It was not all that difficult and only took me about 2 hours in total with refactoring included. If C# was truly cross-platform (mono is not close enough to MS’s implementation), I would really dedicate a fair amount of time to learn it. If you’re developing on an MS platform only, then .Net is the way to go.

Advertisement

2 comments so far

  1. Tom on

    If you use another class and try to reference SomeWebsite, even after doing a SomeWebsite mySite = new SomeWebsite(); – the mySite intelliSense does not pick up public methods and values in _Default. Why is that? What would then be the point of encapsulating it in a namespace? Even after putting a “using SomeWebsite;” at the top of the page in the other class, it does not “see” the _Default class. Is there a workaround?

    -Tom

    • shauntir on

      Sorry, not a C# expert so I’m not sure how to do what you need.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.