Reputation: 61773
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:
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.
Upvotes: 1
Views: 221
Reputation: 21881
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.
Upvotes: 4
Reputation: 25692
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"]);
}
Upvotes: 3