ASP.NET- Instantiate a Web User Control in App_Code class

后端 未结 2 1418
北海茫月
北海茫月 2021-01-19 14:12

Files:

Website\\Controls\\map.ascx

Website\\App_Code\\map.cs

I\'d like to create a strongly typed instance of map.ascx in map.cs

Normally, in

相关标签:
2条回答
  • 2021-01-19 14:31

    Normally, I'd do something like this (assuming your type is "Map" and that you have the appropriate "Inherits" declaration in your .ascx file):

    Map map = (Map)LoadControl("~/Controls/map.ascx");
    
    0 讨论(0)
  • 2021-01-19 14:45

    Is there a map.ascx.cs file in Website\Controls? If so, move it to App_Code. Note you may have to update the CodeFile attribute in the .ascx file to ~\App_Code\map.ascx.cs. Alternatively, since the control is a partial class, you could just create the code in ~\App_Code\map.cs as:

    public partial class controls_Map : UserControl
    {
        protected void Page_Load( object sender, EventArgs e )
        {
             ...code here....
        }
    }
    

    And remove all the methods from the map.ascx.cs file in the controls directory.

    0 讨论(0)
提交回复
热议问题