简体   繁体   English

从母版页后面的代码访问变量

[英]Accessing variable from code behind Master Page

Please see the image below 请看下图

在此处输入图片说明

As you can see I have a master page Design.master and the code behind it that is called Design.master.cs . 如您所见,我有一个母版页Design.master及其背后的代码Design.master.cs

I am simply trying to get my page to redirect to dashboard.aspx when the user is not logged it, but no matter what I try I can't get it do it. 我只是想让我的页面在用户未登录时重定向到dashboard.aspx ,但是无论我尝试什么,都无法做到这一点。

I want to be able to access the value of the public variable redirect , which is set in the code behind and access it from the Design.master code. 我希望能够访问后面代码中设置的公共变量redirect的值,并从Design.master代码中访问它。

So that I can check the variable and put the Response.redirect() code in the Design.master page. 这样我就可以检查变量并将Response.redirect()代码放入Design.master页面中。

Unless you know how to get the Redirect to work from the Design.master.cs code? 除非您知道如何从Design.master.cs代码获取重定向才能工作?

Please help :( It's driving me INSANE! 请帮助:(这让我疯狂!

Updated image 更新图片

阿达斯

Design.master code: Design.master代码:

<%@ Master Language="C#" CodeBehind="~/Design.master.cs" AutoEventWireup="true" %>

<%

        Response.Redirect("default.aspx", true);

%>

<!DOCTYPE html>

<html>

<head>
    <title>Portal</title>
    <link rel="stylesheet" href="styles.css" type="text/css" />

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script src="portal_script.js" type="text/javascript"></script>
</head>
<body>

<div id="main_wrapper">

        <div id="header_top_wrapper">
            <div id="header_top">
                <img src="images/design/logo_small.png" id="logo_top" />

                <a href="basket.aspx">
                    <div id="basket_top_link">
                        <div>Order Basket</div>
                    </div>
                </a>
            </div>
        </div>
        <div id="header_breadcrumb_wrapper">
            <div id="header_breadcrumb"></div>
        </div>

        <div id="content_wrapper">
            <div id="content">

                <div id="side_menu">

                    <div id="customer_info">

                        <asp:ContentPlaceHolder id="CustomerInfo" runat="server">

                        </asp:ContentPlaceHolder>

                    </div>

                    <a href="dashboard.aspx"><div class="side_menu_item">
                        <div class="item_title">View Orders</div>
                        <div class="item_description">View your outstanding orders</div>
                    </div></a>

                    <a href="search.aspx"><div class="side_menu_item">
                        <div class="item_title">Product Search</div>
                        <div class="item_description">Search products and view stock levels</div>
                    </div></a>

                    <a href="search.aspx"><div class="side_menu_item">
                        <div class="item_title">Outstanding Complaints</div>
                        <div class="item_description">View status of complaints</div>
                    </div></a>

                </div>

                <div id="content_left">

                    <asp:ContentPlaceHolder id="Content" runat="server">

                    </asp:ContentPlaceHolder>

                </div>

            </div>
        </div>

        <div id="footer_wrapper">

            <div id="footer"><p></p></div>

        </div>

</div>

</body>
</html>

Design.master.cs code: Design.master.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Design : System.Web.UI.MasterPage
{

    public bool redirect = false;

    protected override void OnInit(EventArgs e)
    {

        // Get the users session
        SessionManager session = new SessionManager(HttpContext.Current);

        // I've tried it here, and it won't redirect
        // Response.Redirect("default.aspx");

        redirect = true;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // I've tried it here, and it won't redirect
        // Response.Redirect("default.aspx");
    }
}

Just do it in your Page_Load method from Design.master.cs : 只需在Design.master.csPage_Load方法中执行此Design.master.cs

protected void Page_Load(object sender, EventArgs e){
   if (result){
      Response.Redirect("....");
   }
}

This is much neater than having code breaks in your markup file, and that technique will only bite you back in the future. 这比在标记文件中破坏代码要整洁得多,而且这种技术只会在将来给您带来麻烦。

AutoEventWireUp is set to false by default. 默认情况下, AutoEventWireUp设置为false。 Looking at your declaration, this is why the master page methods are not wiring up. 查看您的声明,这就是为什么母版页方法未连接的原因。 Change the declaration to 将声明更改为

<%@ Master Language="C#" AutoEventWireup="true"

Then you can add something like 然后,您可以添加类似

protected override void OnInit(EventArgs e)
        {
            if(!loggedIn)
                 Response.Redirect("login.aspx", false);
          //...
        }

For your actual code use: 为您的实际代码使用:

Response.Redirect("Page.aspx",True);

Having the true in your redirect usually fixes those obscure times when it otherwise doesn't work. 重定向中包含true时通常可以解决那些晦涩的情况,否则它将无法正常工作。

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

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