0

伙计们,我想问一下我有一个命名为 page1.ascx 的控件,它有一个工具栏,我有选择和关闭按钮。在工具栏上。我希望当我从 page2.ascx 控件转到 page.ascx 控件时显示该按钮(选择并关闭)但是当我从 page3.ascx 控件转到 Page1.ascx 时不显示该按钮(选择并关闭)怎么做?

4

1 回答 1

0

当您将一个网页重定向到另一个网页时,您正在传递存储在变量中的值,因此您可以通过 4 种方法来做到这一点

1) 会话变量上一页/视图状态变量

2)QueryString http://mysite.com/currentpage?from=previousPage

3)使用类的属性,例如。通过在重定向时设置属性集和在打印时获取。

这是另一种方式。

试试看

public string GetCurrentPageName()
{
    string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
    string sRet = oInfo.Name;
    return sRet;
}


public string GetPreviousPageName()
{
    string sPath = Page.PreviousPage.Request.Url.AbsolutePath;
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
    string sRet = oInfo.Name;
    return sRet;
}

如果你想要上一页标题,那么你可以试试这个:

string PPagetitle= this.Page.PreviousPage.Title;

或者你可以像这样做一点点Javascript:

<asp:button id="m_BackButton" runat="server" onclientclick="goBack()" />

<script type="text/javascript">
   function goBack()
   {
       history.go(-1);
   }
</script>

第三个解释:从一个网页移动到另一个网页时,您可以通过类属性传递数据 - 但它仅适用于 Server.Transfer() 和您需要定义的目标页面

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> 

.这是演示

// 源页面

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div><div>

        <asp:Label ID="lblUsername" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" Style="z-index: 100; left: 240px; position: absolute; top: 32px"
            Text="Username" Width="73px"></asp:Label>
       <br />

        <asp:Label ID="lblPassword" runat="server" BorderStyle="None" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" Style="z-index: 101; left: 237px; position: absolute; top: 80px"
            Text="Password" Width="80px"></asp:Label>
       <br />
       <br />
        <asp:TextBox ID="txtPassword" runat="server" Style="z-index: 102; left: 355px; position: absolute;
            top: 80px" TextMode="Password" Width="151px"></asp:TextBox>
        <asp:TextBox ID="txtUsername" runat="server" Style="z-index: 103; left: 357px; position: absolute;
            top: 30px" Width="153px"></asp:TextBox>
        <asp:Label ID="lblMessage" runat="server" Font-Bold="False" Font-Names="Bookman Old Style"
            Font-Size="Medium" Style="z-index: 104; left: 354px; position: absolute; top: 130px"
            Text="Message :"></asp:Label>
        <asp:Button ID="btnSubmit" runat="server" Font-Bold="True" Font-Names="Garamond"
            Font-Size="Large" OnClick="btnSubmit_Click" Style="z-index: 106; left: 289px;
            position: absolute; top: 160px" Text="Submit" />
    </div>

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

后面的源页面代码:

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    private string myUserName;
    /*
     * Defining Properties in the source page to be Accessible on the destination page.
     * means Exposing data to other pages using Properties
     * To retrieve data from source page,Destination page must have 
     * <%@ PreviousPageType VirtualPath="~/Default.aspx" %> Directive added below <%@ Page %> Directive

     */
    public string propUserName
    {
        get { return myUserName; }
        set { myUserName = value; }
    }
    private string myPassword;

    public string propPassword
    {
        get { return myPassword; }
        set { myPassword = value; }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if ((txtUsername.Text == "chandan") && (txtPassword.Text == "niit"))
        {
            myUserName = txtUsername.Text;
            myPassword = txtPassword.Text;
        }
       Server.Transfer("Description.aspx");
    }
}

目标页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Description.aspx.cs" Inherits="Description" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            &nbsp;
             <asp:Label ID="Label2" runat="server" Text="Password" style="z-index: 100; left: 336px; position: absolute; top: 69px" Font-Bold="True" Font-Size="Larger"></asp:Label>
            <asp:Label ID="Label1" runat="server" Text="UserName" style="z-index: 102; left: 333px; position: absolute; top: 28px" Font-Bold="True" Font-Size="Larger"></asp:Label>
        </div>
    </form>
</body>
</html>

目标页面代码后面:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Description : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = PreviousPage.propUserName;
        Label2.Text = PreviousPage.propPassword;

    }
}
于 2013-11-02T08:04:11.463 回答