asp.net仅从父页面加载子页面

时间:2018-04-13 05:22:15

标签: c# asp.net

我有两页MainPage.aspx和ChildPage.aspx。当我单击按钮时从主页面重定向到ChildPage。 如果我直接在浏览器上提供ChildPage的地址,我不想直接加载它,而是我想重定向到MainPage。 只有从MainPage加载时才必须加载ChildPage。 如何找到ChildPage.aspx的加载位置。如何找到它的父页面或从它的加载位置。

我们可以尝试以下代码中的内容

if (!IsPostBack)
{
   if (finding_source)
   {
       Response.Redirect("MainPage.aspx");
   }
}

2 个答案:

答案 0 :(得分:0)

即使您的问题不明确,我也在尝试提供解决方案。

MAINPAGE.ASPX BUtton点击

 protected void lnkRegister_Click(object sender, EventArgs e)
{
 Session["MainPage"] = "true";//Encrypt it if u wish
Response.Redirect("childpage.aspx");
}

CHILDPAGE.ASPX PAGE LOAD

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 if (!string.IsNullOrEmpty(Session["MainPage"] as string) && Session["MainPage"].Tostring()=="true")
            {
//proceed
}
else
{
 Response.Redirect("mainpage.aspx");
}
}

Global.asax中

void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        Session.RemoveAll();
        Session.Clear();

        Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        Response.Cache.SetValidUntilExpires(false);
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
    }

控制浏览器关闭或关闭是很困难的......但是当您的应用程序关闭时,您可以解决全局asax文件。

答案 1 :(得分:0)

您可以使用Request.UrlReferrer.AbsolutePath查看上一页。

if (!IsPostBack)
{
   if (Request.UrlReferrer != null && Request.UrlReferrer.AbsolutePath == "/MainPage")
   {
     //do what you want
   }else{
      Response.Redirect("~/MainPage.aspx");
   }
}

提示但请注意将其与回发一起使用,因为它会在回发期间将Request.UrlReferrer的值更改为当前页面。

相关问题