从弹出窗口访问asp内容

时间:2012-02-20 23:39:04

标签: c# asp.net

我想从弹出窗口访问asp TreeView。我公之于众,所以我可以访问。 智利代码或Popup Page,

SomeWebPage FlViewPage = new SomeWebPage(); 
lblMessage.Text = FlViewPage.TreeView1.Nodes.Count.ToString();

问题是当我运行WebSite时出现错误:

Object reference not set to an instance of an object.

WebPage(从中调用Popup)代码树动态填充,

    <%@ Page Title="Folder View" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="FolderView.aspx.cs" Inherits="SwimWebFile.FolderView" %>
<div id="content"> <div class="post">
<h1 class="title"> <asp:Label ID="lblTitle" runat="server" Text="Documents"></asp:Label></h1>
    <div class="entry" >
     <!-- Center the pop window in the middle -->
        <script type="text/javascript">
            function PopupCenter(pageURL, title, w, h) {
                var left = (screen.width / 2) - (w / 2);
                var top = (screen.height / 2) - (h / 2);
                var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            } 
        </script>
        <a href="javascript:void(0);" onclick="PopupCenter('NewFolderPopup.aspx', 'Add Document',340,185);">Click Here for Upload</a>
        <font size="4">
        <asp:TreeView ID="TreeViewDocuments" runat="server" ExpandDepth="0" 
            ImageSet="Simple" Visible="False" onprerender="TreeView_PreRender">
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <LeafNodeStyle NodeSpacing="10px" />
            <Nodes>
                <asp:TreeNode Text="System" Value="Systems" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
                <asp:TreeNode Text="Document" Value="Documents" Expanded="False" 
                    SelectAction="Expand">
                </asp:TreeNode>
            </Nodes>
            <NodeStyle Font-Names="Tahoma" Font-Size="Medium" ForeColor="Black" 
                HorizontalPadding="0px" NodeSpacing="7px" VerticalPadding="0px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>
            <asp:Button ID="btnNewFolder" runat="server" Text="New Folder" 
            onclick="btnNewFolder_Click" UseSubmitBehavior="False"/>
            <asp:Button ID="btnRenameFolder" runat="server" Text="Rename Folder" />
            <asp:Button ID="btnDeleteFolder" runat="server" Text="Delete Folder" />
   </font></div>
</div>

代码背后,

protected void Page_Load(object sender, EventArgs e)
    {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) {
                TreeViewDocuments.Visible = false;
                lblTitle.Text = "You need to be logged in.";
            }
            else
                TreeViewDocuments.Visible = true;
    }
    protected void TreeView_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           AddNewNode();
        }
    }
    // Some Code to pop 
     protected void AddNewNode()
     {
      ...
     }
    .
    .
    .
    protected void btnNewFolder_Click(object sender, EventArgs e)
    {
Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "targetWin = window.open('NewFolderPopup.aspx', 'Add Folder', 'toolbar=0, location=0, directories=0, status=0, resizable= 0, menubar=0, copyhistory=no, width=460, height=150');  targetWin.moveTo((screen.height / 2) - (150/ 2), (screen.width / 2) - (460 / 2));", true);
    }

错误,

    Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 56:             FolderView FlViewPage = new FolderView();
Line 57:             lblMessage.Text = FlViewPage.TreeViewDocuments.Nodes.Count.ToString();

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   SwimWebFile.NewFolderPopup.btnCreate_Click(Object sender, EventArgs e) in C:\Users\...\NewFolderPopup.aspx.cs:57
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

2 个答案:

答案 0 :(得分:0)

有很多可能为null我会将调试器连接到它并查看什么是null。此时我的猜测是Treeview1或FlViewPage,但我可能也没有完整的图片

答案 1 :(得分:0)

我找到了解决这个问题的方法。我使用Session方法只接收值而不是整个TreeView。

感谢您的建议!