ASP.Net C # - How to instantiate a WebUserControl object from the App_Code class?

Aloha

I have a custom control that I need to create from a class and add to the page. My class has a function through which I pass Pageso that I have access to the controls on the page. I use this method to add standard ASP controls already and all that it works as expected. My problem is that the type for the custom control is defined as defined?

I read here to move .cs from the control to the App_Code folder, but when I do this, it will not compile since it does not see the controls in ascx as valid. For example, I get CS0103: The name 'litTest' does not exist in the current context. Since they are partial classes, I created a new empty partial class in the App_Code folder and left only the .cs file. Control

Well, it compiles this way, but when I add the control to the Page.controls collection, I don't see anything on the page where it should be. For example, I added TESTan ascx file to the end of the file, I do not see it on the page. In addition, the control has variables that I need to set so that I do not have access to use an empty partial class.

I know that what I'm trying to do works with standard controls, why can't I get this to work with a custom control?

My partial class in the App_Code folder:

public partial class CustomControlClass : System.Web.UI.UserControl
{

}

My partial class for user control:

public partial class CustomControlClass : System.Web.UI.UserControl
{
    private int _myValue= -1;
    public int myValue
    {
        get { return _myValue; }
        set { _myValue= value; }
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        litTest.Text = "TEST";
    }
}

My ascx user interface:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControlClass.ascx.cs" Inherits="CustomControlClass" %>
<asp:Literal ID="litTest" runat="server"></asp:Literal>
TEST

My class is App_Code:

public class MyClass
{
    public static void doWork(Page Ctrl)
    {
        CustomControlClass c = new CustomControlClass();
        //c.myValue = 1;  // Wont compile with this line
        Ctrl.Controls.Add(c);

        Literal l = new Literal();
        l.Text = "HELLO";
        Ctrl.Controls.Add(l);
    }
}

The word TEST is not displayed on the page output. The word "HELLO" appears just fine. I tried calling MyClass.doWork()from pages Load, Initand PreInitcallbacks, they all lead to a date.

Update:

As Min Nguyen said, I can use the Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");control to load, but I cannot use it as my own type. I have to assign it as a control type:

Control c= Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");

. , - . , :

c.GetType().GetProperty("myValue").SetValue(c, 1, null);

, , . , , .

, , .

+3
3

CustomControlClass c = (CustomControlClass)Ctrl.LoadControl("~/VirtualPathToASCX/CustomControlClass.ascx");

CustomControlClass c = new CustomControlClass();

: ​​

//LoadControl
ASP.customcontrolclass_ascx c = (ASP.customcontrolclass_ascx)this.LoadControl("~/Controls/CustomControlClass.ascx");
//set myValue
c.myValue = 3;

ASP.customcontrolclass_ascx VS AutoComplete List, .

+4

, , , , :

< > ~//testControl.ascx:

<%@ Control Language='C#' AutoEventWireup='false' 
  Inherits='CustomControlClass' 
%>
<asp:Literal ID='litTest' runat='server'></asp:Literal>

.aspx :

<%@ Page Language='C#' %>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<script runat='server'>
protected override void OnInit(EventArgs e) {
  base.OnInit(e);
  MyClass.doWork(this.Page);
}
</script>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'><title></title></head>
<body><form id='form1' runat='server'>
</form></body></html>

~/App_Code/CustomControlClass.cs

using System;
using System.Web;
using System.Web.UI.WebControls;

public partial class CustomControlClass : System.Web.UI.UserControl {
  private int _myValue= -1;
  public int myValue {
    get { return _myValue; }
    set { _myValue= value; }
  }
  private Literal _litTest;
  public Literal litTest {
    get { return _litTest; }
    set { _litTest= value; }

  }
  protected override void  OnInit(EventArgs e) {
    base.OnInit(e);
    litTest.Text = "TEST";
  }
}

~/App_Code/MyClass

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class MyClass {
  public static void doWork(Page Ctrl) {
    CustomControlClass c = 
      (CustomControlClass) Ctrl.LoadControl("~/controls/testControl.ascx");
    c.myValue = 1;
    Ctrl.Form.Controls.Add(c);

    Literal l = new Literal();
    l.Text = string.Format(
      "<p>CustomControlClass.myValue: {0} </p>", 
      c.myValue
    )
    + string.Format(
      "<p>CustomControlClass.litTest.Text: {0} </p>", 
      c.litTest.Text
    )
    ;
    Ctrl.Form.Controls.Add(l);
  }
}

, CustomControlClass LoadControl() . , ?

+1

LoadControl, ( .NET 4)

,

:
protected void Page_Load(object sender, EventArgs e)
{
    Control ctrl = SomeClass.GetNewUserControl( "hello add me");

    Panel1.Controls.Add(ctrl);
}

~/App_Code/BaseMyControls.cs:

using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for BaseMyControls
/// </summary>
public class BaseMyControls  : System.Web.UI.UserControl
{
    public string strProperty;

    public BaseMyControls()
    {

    }
}

~/App_Code/SomeClassInApp_Code.cs:

using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for SomeClassInApp_Code
/// </summary>
public class SomeClassInApp_Code
{
    public static System.Web.UI.Control GetNewUserControl(string someString)
    {
        BaseMyControls bc = (BaseMyControls)(new System.Web.UI.UserControl()).LoadControl("~/controls/MyUC.ascx");
        bc.strProperty = someString;
        return bc;
    }
}

~//MyUC.aspx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUC.ascx.cs" Inherits="MyUC" %>
<asp:Label runat="server" ID="lbl" /></asp:Label>

~//MyUC.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyUC : BaseMyControls
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string a = this.strProperty;

        lbl.Text = a;
    }
}
+1

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


All Articles