ASP.NET Create a web user control in the App_Code class.

Files:

Website \ Controls \ map.ascx

Website \ App_Code \ map.cs

I would like to create a strongly typed map.ascx instance in map.cs

Usually in aspx you add the <% Register ... tag to be able to instantiate in code. Is this possible in the app_code class? I am using .NET 3.5 / Visual Studio 2008

Thanks!

+4
source share
2 answers

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

Map map = (Map)LoadControl("~/Controls/map.ascx"); 
+4
source

Is there a map.ascx.cs file in the \ Controls website? If so, move it to App_Code. Note that you may need to update the CodeFile attribute in the .ascx file to ~ \ App_Code \ map.ascx.cs. Alternatively, since the control is a partial class, you can simply 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 methods from the map.ascx.cs file in the control directory.

+1
source

Source: https://habr.com/ru/post/1277566/


All Articles