开发者

ASP.net c# page_load firing twice?

开发者 https://www.devze.com 2023-01-10 15:56 出处:网络
protected void Page_Load(object sender, EventArgs e) { string menuIDdata = Page.Request.QueryString[\"mid\"];
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;
    }

    debugLabel.Text = "WORKING";

    var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;
    selectedMenu.CssClass = "navButtonO";
}   

And on the page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="admin.aspx.cs" Inherits="*******._Default" title="Administrat开发者_如何学Pythonion" %>

<!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>

    <form id="mainForm" runat="server">

    <div class="topMenu">        
        <asp:Panel id="mnu0" runat="server" class="navButton">
            <a href="admin.aspx" class="navLink">Admin Home</a>
        </asp:Panel>

        <asp:Panel id="mnu1" runat="server" class="navButton">
            <a href="admin.aspx" class="navLink">User Manager</a>
        </asp:Panel>

        <asp:Panel id="mnu2" runat="server" class="navButton">
            <a href="admin.aspx" class="navLink">Products</a>
        </asp:Panel>              
    </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
    <br /><br />        
    <asp:label id="debugLabel" runat="server" />

    </form>
    </body>
</html>

The debug label refuses to change it's value, I'm expecting it to show "WORKING" as it's text, what am I doing wrong?

Update:

When attempting to build I get 3 errors:

Error 1 Type '******._Default' already defines a member called 'Page_Load' with the same parameter types\Default.aspx.cs 12 24

x3 but on different pages


EDIT:

Ok here you go. You have a Default.aspx page that the Admin-Page most probably got copied from.

public partial class _Default : System.Web.UI.Page 

So if you need some base functions from this Default-Page you need to inherit from this Page. Then change it like this:

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

If that is not the case... (Asuming that the class of the pages is called like its file...)

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

Change your code...

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

Since TryParse is returning the parsed integer with the output paramter.


Put a debugger on the code and you would see that the code never reaches the " debugLabel.Text = "WORKING"; " line of code because of some exception happening before it. There's absolutely no problem with the way you are assigning the text to label


try
{
string menuIDdata = Page.Request.QueryString["mid"];
menuID = 0;

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

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

var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;
selectedMenu.CssClass = "navButtonO";


This line:

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

Confuses me. Do you have a file called admin.aspx.cs with a public class called *******._Default? That is a very unusual convention. Perhaps your codebehind reference or class name that this page inherits from is incorrect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号