CodeFileBaseClass in @Control and @Page directives

Specifies the type name of a base class for a page and its associated code-behind class. This attribute is optional, but when it is used the CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario, where you define common fields (and optionally, associated events) in a base class to reference the controls declared in a Web page. Because of the ASP.NET code generation model, if you defined the fields in a base class without using this attribute, at compile time new member definitions would be generated for the controls declared in the Web page (within a separate partial class stub), and your desired scenario would not work. But if you use the CodeFileBaseClass attribute to associate the base class with the page, and you make your partial class (its name is assigned to the Inherits attribute and its source file is referenced by the CodeFile attribute) inherit from the base class, then the fields in the base class will be able to reference the controls on the page after code generation.

The post demonstrating how to use a base class from a web form with inline code. We need a slightly different approach for web forms using code-beside.

A simple example is like this...

Ferret.ascx
<%@ Control Language="C#" CodeFileBaseClass="BaseFerret" AutoEventWireup="true" CodeFile="Ferret.ascx.cs"
Inherits="Ferret" %>
<asp:Literal ID="litFerretName" runat="Server" />

Ferret2.ascx

<%@ Control Language="C#" CodeFileBaseClass="BaseFerret" AutoEventWireup="true" CodeFile="Ferret2.ascx.cs"
Inherits="Ferret" %>
<asp:Literal ID="litFerretName" runat="Server" />


Steps to make it work

1. Create a class into App_Code

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

public class BaseFerret : System.Web.UI.UserControl
{
protected Literal litFerretName;

public void DisplayFerret()
{

litFerretName.Text = "hello from base class";
}

}
2. Modify Ferret.ascx.cs class to derive from this
public partial class Ferret : BaseFerret
{}
3. Modify @Control directive in both user controls so that it additionally contains
< % @Control ... CodeFileBaseClass="BaseFerret"  % >



check out this complete code


Ferret.ascx
<%@ Control Language="C#" CodeFileBaseClass="BaseFerret" AutoEventWireup="true" CodeFile="Ferret.ascx.cs"
Inherits="Ferret" %>
<asp:Literal ID="litFerretName" runat="Server" />
Ferret.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Ferret : BaseFerret
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
Ferret2.ascx
<%@ Control Language="C#" CodeFileBaseClass="BaseFerret" AutoEventWireup="true" CodeFile="Ferret2.ascx.cs"
Inherits="Ferret2" %>
<asp:Literal ID="litFerretName" runat="Server" />
code behind
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Ferret2 :BaseFerret
{
 protected void Page_Load(object sender, EventArgs e)
 {

 }
}
Now, in the calling page, I have a Placeholder called plcVole. The code in the code-behind for the page looks like this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CodeFileBase.aspx.cs" Inherits="CodeFileBase" %>

<!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>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:PlaceHolder ID="plcVole" runat="Server"></asp:PlaceHolder>
      </div>
  </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CodeFileBase : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {


       BaseFerret ctlFerret1 = (BaseFerret)LoadControl("Ferret.ascx");
     
       ctlFerret1.DisplayFerret();

       plcVole.Controls.Add(ctlFerret1);

       BaseFerret ctlFerret2 = (BaseFerret)LoadControl("Ferret2.ascx");

       ctlFerret2.DisplayFerret();

       plcVole.Controls.Add(ctlFerret2);
   }
}

Reactions

Post a Comment

0 Comments

Close Menu