简体   繁体   English

ASP.net C#基本问题

[英]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. 愿意承认我是一个完整的.NET新手,但是我在经典ASP方面拥有丰富的经验,由于.net的整个结构完全不同,因此这变得非常棘手。

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 以“正确”的方式完成所有工作可能需要太多步骤,随着我逐渐掌握.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. 因此,请原谅我缺乏代码,在此页面上,我试图获取由查询字符串“ mid”(菜单ID)返回的ID,然后为我们当前使用的菜单按钮显示不同的CSS类。 Two menu classes, navButton and navButtonO (over). 两个菜单类navButton和navButtonO(在上方)。

<%@ 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. 其次,您仍在考虑经典的ASP,并使用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. 几乎没有理由使用Response.Write,如果您在标记页面中使用它,那么您几乎总是在做错事情。 Turn your divs into Panel controls which will render out as divs. 将div转换为Panel控件,将其呈现为div。 Then use a simple switch statement to set the CssClass property in the code behind page. 然后,使用简单的switch语句在页面后面的代码中设置CssClass属性。 You are using int.Parse you should only use this if you are guaranteed to get an int back from parsing the text. 您正在使用int.Parse ,只有在保证可以从解析文本中获得int的情况下才应使用此方法。 If it does not parse it will throw an exception, use int.TryParse instead. 如果不解析,将引发异常,请改用int.TryParse。

Promote midID to a class variable. midID提升为类变量。

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;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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