简体   繁体   中英

ASP.net c# basic question

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.

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.

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"]);
}     
int menuId = 0;

应该:

public int MenuId{get;set;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM