使用“PreviousPage”从源页面获取值

时间:2014-02-27 08:32:18

标签: c# asp.net

我尝试使用PreviousPage技术在两个网络表单之间发布值 我已经关注了关于PreviousPage的MSDN文章(http://msdn.microsoft.com/en-us/library/ms178139.aspx),我也引用了它(http://www.deliciousdotnet.com/2011/03/getting-values-from-source-page-using.html#.Uw7jCvmSz3Q)。所有似乎都井然有序,但我看到以下“System.Web.UI.Page的未知成员'指定”关于目的地页面中的公共方法。

我做错了什么?请帮忙。谢谢。

这是我的源页面html代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="adduser.aspx.cs" Inherits="adduser" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!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></title>
    <link href="../Styles/main_style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body style="min-height: 600px; background-image: none !important;">
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <div class="lightboxContainer">
        <div class="lightboxContainerSection">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table cellpadding="5" width="600px" class="style1" style="font-size: 12px; margin: 5px;
                        font-weight: bold;">
                        <tr>
                            <td>
                                Find User
                            </td>
                            <td>
                                <telerik:RadTextBox ID="RadTextBox1" runat="server" ValidationGroup="textbox1" OnTextChanged="TextBox1_TextChanged">
                                </telerik:RadTextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadTextBox1"
                                    ErrorMessage="*" Style="color: #FF0000" ValidationGroup="textbox1"></asp:RequiredFieldValidator>
                            </td>
                            <td>
                                <telerik:RadButton ID="RadButton1" runat="server" Text="Find User" OnClick="RadButton1_Click"
                                    ValidationGroup="textbox1">
                                </telerik:RadButton>
                            </td>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <telerik:RadGrid ID="RadGrid_User" runat="server" OnNeedDataSource="RadGrid_User_NeedDataSource"
                                    OnSelectedIndexChanged="RadGrid_User_SelectedIndexChanged" AllowPaging="True"
                                    CellSpacing="0" GridLines="None">
                                    <ClientSettings EnablePostBackOnRowClick="True">
                                        <Selecting AllowRowSelect="True" />
                                    </ClientSettings>
                                </telerik:RadGrid>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td>
                                User name
                            </td>
                            <td>
                                <asp:Label ID="LabelUser" runat="server" Text="Label"></asp:Label>
                            </td>
                            <td>
                                Company
                            </td>
                            <td>
                                <asp:Label ID="LabelCompany" runat="server" Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Designation
                            </td>
                            <td>
                                <asp:Label ID="LabelDesignation" runat="server" Text="Label"></asp:Label>
                            </td>
                            <td>
                                Department
                            </td>
                            <td>
                                <asp:Label ID="LabelDepartment" runat="server" Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Mobile
                            </td>
                            <td>
                                <asp:Label ID="LabelMobile" runat="server" Text="Label"></asp:Label>
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <asp:Label ID="LabelConfirmation" runat="server" Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <telerik:RadButton ID="RadButton2" runat="server" onclick="RadButton2_Click" 
                                    Text="Yes">
                                </telerik:RadButton>
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <hr />
    </div>
    </form>
</body>
</html>

这是我文件背后的源页面代码

using System;
using System.Linq;
using Telerik.Web.UI;

public partial class adduser : System.Web.UI.Page
{
    private ActiveDirectory ad = new ActiveDirectory();
    private DatabaseConnect db = new DatabaseConnect();

    public void RadGrid_User_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridDataItem selectedItem = (GridDataItem)RadGrid_User.SelectedItems[0];
        string user = selectedItem["Email"].Text;

        Session["userID"] = user.Split('@')[0];

        RadTextBox1.Text = (string)Session["userID"];

        string detail = ad.GetUserDetails(RadTextBox1.Text.Trim());
        string[] details = detail.Split('/');

        LabelUser.Text = details[0];
        LabelCompany.Text = details[1];
        LabelDepartment.Text = details[3];
        LabelDesignation.Text = details[4];
        LabelMobile.Text = details[5];

        LabelConfirmation.Text = "Do you want to grant "+details[0]+" permission to access the CRI ?";

        LabelUser.Visible = true;
        LabelCompany.Visible = true;
        LabelDepartment.Visible = true;
        LabelDesignation.Visible = true;
        LabelMobile.Visible = true;
        LabelConfirmation.Visible = true;
        RadButton2.Visible = true;
    }

    public void RadGrid_User_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        string username = this.RadTextBox1.Text.Trim();
        if (username.Length >= 1)
        {
            this.RadGrid_User.DataSource = this.ad.GetUserDetails_WildCard(this.RadTextBox1.Text.Trim());
        }
        else
        {
            this.RadGrid_User.Visible = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.db.IsAllowedAdministrator(this.User.Identity.Name))
        {
            if (!this.IsPostBack)
            {
                LabelCompany.Visible = false;
                LabelDepartment.Visible = false;
                LabelDesignation.Visible = false;
                LabelMobile.Visible = false;
                LabelUser.Visible = false;
                LabelConfirmation.Visible = false;
                RadButton2.Visible = false;
            }
        }
        else
        {
            this.Response.Redirect("~/ZCRI_RestrictedAdmin.aspx");
        }
    }

    protected void RadButton1_Click(object sender, EventArgs e)
    {
        this.RadGrid_User.Rebind();
        this.RadGrid_User.Visible = true;
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        LabelCompany.Visible = false;
        LabelDepartment.Visible = false;
        LabelDesignation.Visible = false;
        LabelMobile.Visible = false;
        LabelUser.Visible = false;
        LabelConfirmation.Visible = false;
        RadButton2.Visible = false;
    }

    public string Designation
    {
        get
        {
            return LabelDesignation.Text;
        }
    }

    public string Mobile
    {
        get
        {
            return LabelMobile.Text;
        }
    }

    public string Company
    {
        get
        {
            return LabelCompany.Text;
        }
    }

    public string Department
    {
        get
        {
            return LabelDepartment.Text;
        }
    }
    protected void RadButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/AdminInterfaces\adduserpermission.aspx");
    }
}

这是我的目标网页

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="adduserpermission.aspx.cs"
    Inherits="adduserpermission" %>
<%@ PreviousPageType VirtualPath="~/AdminInterfaces/adduser.aspx" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!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 id="Head1" runat="server">
    <title></title>
    <link href="../Styles/main_style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body style="min-height: 600px; background-image: none !important;">
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <div class="lightboxContainer">
        <div class="lightboxContainerSection">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:Label ID="Label2"
                runat="server" Text="Label"></asp:Label><asp:Label ID="Label3"
                runat="server" Text="Label"></asp:Label><asp:Label ID="Label4"
                runat="server" Text="Label"></asp:Label>
        </div>
    </div>
    </form>
</body>
</html>

我的目标页面代码隐藏在文件

之后
using System;
using System.Linq;

public partial class adduserpermission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = PreviousPage.Designation;
        Label2.Text = PreviousPage.Department;
        Label3.Text = PreviousPage.Company;
        Label4.Text = PreviousPage.Mobile;

    }
}

2 个答案:

答案 0 :(得分:0)

现在这将起作用,因为我将标签与您的属性混淆。

在上一页上,您有一个名称为LabelDesignation的标签,但在这里您使用了错误的名称来访问它。试着用这个。

您在文件

后面的源代码页代码中遇到此代码的问题
this.Response.Redirect("~/ZCRI_RestrictedAdmin.aspx");

PreviousPage属性返回使用Server.Transfer将控件发送到此页面的页面。

如果当前页面是由于直接请求而呈现的(不是来自其他页面的传输或交叉发布),则PreviousPage属性包含null。

    else
    {
        this.Server.Transfer("~/ZCRI_RestrictedAdmin.aspx");
    }



using System;
using System.Linq;

public partial class adduserpermission : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (Page.PreviousPage != null)
       {
         if(Page.PreviousPage.IsCrossPagePostBack == true)
         {
            Label1.Text = PreviousPage.Designation;
            Label2.Text = PreviousPage.Department;
            Label3.Text = PreviousPage.Company;
            Label4.Text = PreviousPage.Mobile;
         }
      }
   }
}

答案 1 :(得分:-1)

您需要将其转换为正确的类型:

public partial class adduserpermission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        adduser prevPage = PreviousPage as adduser;
        if (prevPage != null)
        {
            Label1.Text = prevPage.Designation;
            Label2.Text = prevPage.Department;
            Label3.Text = prevPage.Company;
            Label4.Text = prevPage.Mobile;
        }
    }
}