1

Willing to admit I'm a complete .NET newbie, but I have extensive experience in classic ASP, which is making this quite tricky as the whole structure of .net is completely different.

I know I'm meant to use code behind, but for now I'm happy embedding it into the pages because:

  1. Each page is going to be simple, so there wont be too much mixing up
  2. It's probably too much of a step to do everything the 'right' way, I'd rather step up to that slowly as I get to grips with .net

So excusing my lack of code behind, on this page I am trying to get the ID returned by the querystring "mid" (Menu ID), and then display a different CSS class for the menu button we are currently on. Two menu classes, navButton and navButtonO (over).

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="admin.aspx.cs" Inherits="AlphaPack._Default"
    title="Administration"
%>

<script language="C#" runat="server" >

    protected int menuID;

    protected void Page_Load(object sender, EventArgs e)
    {
        string menuIDdata = Page.Request.QueryString["mid"];
        menuID = 0;

        // Check the user is allowed here
        if (!Roles.IsUserInRole("Admin"))
        {
            Response.Redirect("../default.aspx");
        }

        // Get the menu ID
        if (int.TryParse(menuIDdata, out menuID))
        {
            menuID = int.Parse(menuIDdata);
        }else{
            menuID = 0; 
        }

    }     
</script>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
    1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="mainHead" runat="server" >
        <title>Administration</title>
        <link rel="Stylesheet" href="../style/admin.css" />       
    </head>
    <body>

    <div class="topMenu">    
        <div class="navButton<%if(menuID == 0){ response.write("O") }%>">
            <a href="admin.aspx" class="navLink">Admin Home</a>
        </div>  
        <div class="navButton<%if(menuID == 1){ response.write("O") }%>">
            <a href="users.aspx" class="navLink">User Manager</a>
        </div>    
        <div class="navButton<%if(menuID == 2){ response.write("O") }%>">
            <a href="products.aspx" class="navLink">Products</a>
        </div>      
    </div>
    <br /><br />
    <div class="subMenu">
        <a href="products.aspx" class="subLink">Products</a> <a href="productCats.aspx" class="subLink">Categories</a> 
    </div>

    <br /><br />
    Welcome to the Admin

    </body>
</html>

Thanks for any help, don't pull any punches.

5
  • What's the question? Is something not working? Commented Aug 9, 2010 at 13:28
  • 1
    As a slight aside, doing things the 'half way' right way is just going to make your life harder in the long run... Better to jump in, I think. Commented Aug 9, 2010 at 13:32
  • I get 3 errors: ; expected Invalid expression term '}' ) expected All on the </script> line Commented Aug 9, 2010 at 13:32
  • @Paddy, I know you could be right, but I think I'll be ok :) As soon as I get my head around all the syntax and structure then I'll go straight in, it's just too much of a change for my peanut brain all in one go apparently :( Commented Aug 9, 2010 at 13:33
  • By the way, it makes it easier to read if you change <%if(menuID == 2){ response.write("O") }%> to <%= menuID == 2 ? "O" : string.Empty %> Commented Aug 9, 2010 at 13:50

3 Answers 3

4

You should really put your code in the code behind page, there is no value to keeping it in the markup page even if it is simple. Second you are still thinking classic asp and using Response.Write. There is almost no reason to ever use Response.Write, if you are using it in a markup page then you are almost always doing it wrong. Turn your divs into Panel controls which will render out as divs. Then use a simple switch statement to set the CssClass property in the code behind page. You are using int.Parse you should only use this if you are guaranteed to get an int back from parsing the text. If it does not parse it will throw an exception, use int.TryParse instead.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. Use codebehind files and things get easier. Pages can hardly be so simple to not have a gain from the visual split.
Thanks, using pannel controls now and it all seems to be working great :)
You don't need the If Else for the int.TryParse, if it succeeds menuID will be set to the parsed value, if it fails it will set it to 0.
3

Promote midID to a class variable.

protected int menuID;

protected void Page_Load(object sender, EventArgs e)
{
   menuID = 0;

    // Check the user is allowed here
    if (!Roles.IsUserInRole("Admin"))
    {
        Response.Redirect("../default.aspx");
    }

    // Get the menu ID
    menuID = int.Parse(Page.Request.QueryString["mid"]);
}     

1 Comment

Thanks, I get a ) expected error on the line that has the </script> tag though!
0
int menuId = 0;

Should be:

public int MenuId{get;set;}

2 Comments

The name 'menuID' does not exist in the current context
This is why I am struggling with .net, I post a few questions and nothing ever seems to work for me :-(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.