Why HttpContext.Current.Handler is null?

前端 未结 4 979
长发绾君心
长发绾君心 2020-12-18 00:42

I\'m trying to access a Page within an HttpModule and I think I should do this by calling HttpContext.Current.Handler (This should reference the current page) but I\'m getti

4条回答
  •  忘掉有多难
    2020-12-18 01:26

    I have similar problems and finally found solution . my problem was returned null then use this code on external class . I apologize for my English is not good .

    solution via code :(Have Tested)
    Tested by: VS 2010

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    
    //[Description of MyNamespace]
    //|================================================================================>
    //|-----*(In The Name Of GOD)*-----
    //|================================================================================>
    
    namespace MyNamespace
    {
    //Most Be "partial class" And ": System.Web.UI.Page" !!!!
    public partial class MyClass : System.Web.UI.Page
    {
        //|============================================================>
        //| Value Of Class.
        //|============================================================>
    
        static System.Web.UI.Page Page1 = null;
        static System.Web.UI.Page Page2 = null;
    
        int form1Index = -0;
    
    
        //Most Be Static Method!!!!
        public static void GetMyPage()
        {
            //Both are a result code.
            //هر دو کد یه نتیجه می دهد
            Page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            Page2 = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
    
        }
    
    
        //|============================================================>
        //| DO() Methods Of MyClass Class.
        //|============================================================>
        public void DO()
        {
            //Call Your Static Method => GetMyPage()
            GetMyPage();
    
            if (Page1 != null)
            {
                for (int i = 0; i < Page1.Controls.Count; i++)
                {
                    if (Page1.Controls[i].ID == "form1")
                    {
                        form1Index = i;
                        break;
                    }
                }
            }
    
            if (form1Index != -0)
            {
                for (int j = 0; j < Page1.Controls[form1Index].Controls.Count; j++)
                {
                    string ControlsID = Page1.Controls[form1Index].Controls[j].ID;
                    // Code location ...
                    //محل قرار گیری کد ها...
                }
    
            }
        }
    
    
    
        //|============================================================>
        //| Destructor Methods MyClass Class.
        //|============================================================>
        ~MyClass() { }
    }
    

    }

提交回复
热议问题