Access to base class property when using a DirectMethod

  1. #1

    Access to base class property when using a DirectMethod

    In our web application that we're looking at rewriting using Ext.Net, our pages are all subclassed from a PageBase that gives us common access across all pages to the logged in user, employee object, etc. that are stored in Session or loaded each time in context.

    When using a DirectMethod, I have access to Session directly and all instance variables on my page, but everything in the base class (PageBase) is null (although normally available during PostBacks. So, referencing the same user object that is in session that I can access directly gives me a null reference error when using the PageBase user property.

    Is there something that I need to set in the web.config to make this work correctly?

    Thanks!
    Jason
    Logic Speak
  2. #2
    Hi Jason,

    I'm going to run a few tests, to try and reproduce this issue, although we might need a code sample from you demonstrating how to reproduce.

    In what Page event are you adding the User object into the Session?

    Actually, can you just post some code which demonstrates to us how to get your scenario working locally? I think we might be spinning our wheels trying to guess what you are doing.
    Geoffrey McGill
    Founder
  3. #3
    Geoff,

    Looks like the site is back up...see below for snippets:

    Hope this helps...I just included snippets from each file since there is a lot of other stuff going on (that was unrelated to the issue)...

    The DirectMethod that is having the problem is menuNew_NewRequest_Click in the SplitButton menu below.
    Portal.aspx
    ...
        <ext:Viewport ID="ViewPort1" runat="server">
            <Items>
                <ext:BorderLayout ID="BorderLayout1" runat="server">
                    <North Split="false" Collapsible="false">
                        <ext:Panel ID="pnlHeader" runat="server" Title="Header" Header="false" Border="false">
                            <BottomBar>
                                <ext:Toolbar runat="server">
                                    <Items>
                                        <ext:SplitButton ID="btnNew" runat="server" Text="New" Icon="Add" IconAlign="Left">
                                            <Menu>
                                                <ext:Menu ID="menuNew" runat="server">
                                                    <Items>
                                                        <ext:MenuItem ID="NewRequest" runat="server" Icon="PageAdd" Text="Request">
                                                            <DirectEvents>
                                                                <Click OnEvent="menuNew_NewRequest_Click"></Click>
                                                            </DirectEvents>
                                                        </ext:MenuItem>
                                                        <ext:MenuItem ID="NewProject" runat="server" Icon="PluginAdd" Text="Project">
                                                        </ext:MenuItem>
                                                    </Items>
                                                </ext:Menu>
                                            </Menu>
                                        </ext:SplitButton>
                                    </Items>
                                </ext:Toolbar>
                            </BottomBar>
                        </ext:Panel>
                    </North>
                </ext:BorderLayout>
            </Items>
        </ext:Viewport>
    The Portal.aspx.cs codebehind page actually is of type PageBase (our own class) instead of System.Web.UI.Page (PageBase.cs inherits from System.Web.UI.Page)
    Portal.aspx.cs
    ...
            protected void menuNew_NewRequest_Click(object sender, DirectEventArgs e)
            {
    		newForm = new UserForm();
    		//the following line errors out with a null reference exception
    		newForm.UserID = base.UserInfo.UserId;
    		
    		//however, the following lines work just fine
    		User formUser = Session["User"] as User;
    		newForm.UserID = formUser.UserId;
            }
    ...

    PageBase.cs has the same user Session object as a public property...
    PageBase.cs
    ...
            public User UserInfo
            {
                get { return base.Context.Session["User"] as User; }
                set { base.Context.Session["User"] = value; }
            }
    ...
  4. #4
    Geoff,
    Did the code I included help? Were you able to find out anything?

    Thanks!
    Jason
  5. #5
    Hi,

    What if you change the property like below (remove base.Context)
    public User UserInfo
    {
        get { return Session["User"] as User; }
        set { Session["User"] = value; }
    }
    Can you provide test solution (without assemblies)? DirectEvents doesn't affect on Session therefore it is very strange issue on your side
  6. #6
    Vlad,

    Thanks for the follow up!

    I will try to remove the base.Context and see what happens. The strange thing is that this is existing code that without using the Ext.Net DirectMethod (a normal postback) works just fine. The UserInfo property is available and works with no problem.

    Jason
  7. #7
    Vlad,

    You were right...removing the use of Context.Session fixed it because when doing a DirectMethod, there is no postback and hence no Context (that's why Context was returning null, but Session still worked). Thanks so much for the help! Problem solved!!!!

    Jason

    Quote Originally Posted by logicspeak View Post
    Vlad,

    Thanks for the follow up!

    I will try to remove the base.Context and see what happens. The strange thing is that this is existing code that without using the Ext.Net DirectMethod (a normal postback) works just fine. The UserInfo property is available and works with no problem.

    Jason
  8. #8
    Hi Jason,

    I'm happy this is now working, but I still have a few questions because I'm not entirely sure why your original code using base.Context does not work.

    In your base Page, where (which event) are you setting base.Context.Session["User"] (set .UserInfo property)?

    Did you have a specific reason for using base.Context.Session originally, instead of base.Session or just this.Session? I'll have to do some research into the purpose of base.Context.Session.
    Geoffrey McGill
    Founder
  9. #9
    I ran a few more tests and I'm just not sure why your original code does not work. I think it must rely on where you're setting the .UserInfo property.

    With base.Context.Session, the Session object is only going to be available for that specific request. Maybe the problem was just a typo in your code, because using base.Context.Session appears to work as expected with both DirectEvents and DirectMethods.

    Here's what I was testing with.

    Example (.cs Base Page)

    using System.Web.UI;
    
    namespace CompanyX
    {
        public class XPage : Page
        {
            public string Name
            {
                get
                {
                    return base.Context.Session["Name"] as string;
                }
                set
                {
                    base.Context.Session["Name"] = value;
                }
            }
    
            protected override void OnInit(System.EventArgs e)
            {
                this.Name = "Mr. Brown";
    
                base.OnInit(e);
            }
        }
    }
    Example (.aspx)

    <%@ Page Language="C#" Inherits="CompanyX.XPage" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
     
    <script runat="server">
        protected void Button1_Click(object sender, DirectEventArgs e)
        {
            X.Msg.Notify("Button1_Click", base.Name).Show();
        }
        
        [DirectMethod]
        public void DoSomething()
        {
            X.Msg.Notify("DoSomething", base.Name).Show();
        }
    </script>
     
    <!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>Ext.NET Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
             
            <ext:Button
                runat="server" 
                Text="DirectClick" 
                OnDirectClick="Button1_Click" 
                />
                
            <ext:Button 
                runat="server" 
                Text="DirectMethod" 
                OnClientClick="Ext.net.DirectMethods.DoSomething();" 
                />
        </form>
    </body>
    </html>
    Geoffrey McGill
    Founder
  10. #10
    Geoff,

    This is code that has worked for a long time with normal ASP.net postbacks...it only stopped when I started using the directmethods, etc. The only other complication from what I posted is that my PageBase.cs is actually in another dll (we have a set of framework dlls that store a lot of our common code).

    So, the page/inheritance hierarchy actually is:

    MasterPage
    -> ASPX Page
    -> PageBase (in our web application)
    -> PageBase (in our framework)
    -> System.Web.UI.Page

    I know it sounds confusing, but it really helps us develop reusable code that dramatically cuts down on the amount of coding we have to do for each of our projects. The UserInfo was actually in the Framework PageBase, but even objects stored in the web application (in our site, EmployeeInfo for example) were null as well.

    If you like, I can strip out just the code that is relevant and send you compilable examples. I solved it by removing base.Context and just referencing Session in the code, but I'm not sure why that works...

    Let me know if you'd like for me to send you compilable code to look at!

    Thanks again,
    Jason

Similar Threads

  1. Replies: 1
    Last Post: Apr 30, 2012, 7:34 AM
  2. Replies: 4
    Last Post: Apr 25, 2012, 11:57 AM
  3. Replies: 5
    Last Post: Apr 19, 2012, 4:35 PM
  4. Replies: 2
    Last Post: Mar 28, 2011, 9:20 AM
  5. Replies: 0
    Last Post: Sep 21, 2010, 7:33 PM

Tags for this Thread

Posting Permissions