Web user controls in different projects

I put some of my own web user controls in a separate project called "WebControls" and now I want to reuse them from another project

My Control consists of:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="WebControls.TestControl" %> <asp:Label ID="lblTest" runat="server" ></asp:Label> <asp:TextBox ID="textBox" runat="server" Width="" /> <asp:HiddenField ID="hiddenFieldId" runat="server" /> 

with code:

 namespace WebControls { public partial class TestControl : System.Web.UI.UserControl { public Unit Width { get { return textBox.Width; } set { textBox.Width = value; } } public string SelectedId { get { return hiddenFieldId.Value; } set { hiddenFieldId.Value = value; } } public string SelectedText { get { return textBox.Text; } set { textBox.Text = value;} } protected void Page_Init(object sender, EventArgs e) { } protected void Page_Load(object sender, EventArgs e) { } } } 

I am binding it to a webpage in another project:

 <%@ Page Title="ToDo Serien" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="RecurringToDos.aspx.cs" Inherits="TestAccess.RecurringToDos" %> <%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="aci" %> <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent"> <section class="featured"> <div class="content-wrapper"> <hgroup class="title"> <h1><%: Title %>.</h1> <h2>Serienelement</h2> <aci:TestControl ID="aceRule" runat="server" Width="300" /> <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" /> </hgroup> </div> .... 

When I start the page now, it throws a reference null exception in the following line:

 set { textBox.Width = value; } 

becuase textBox = NULL

It seems that my control is not running properly. What am I doing wrong? How can i fix this?

+7
source share
1 answer

If you want to reuse the ascx user control for multiple projects, you must copy the ascx file to the consumer project and register the tag as follows:

 <%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %> 

As an example, you can do the following:

  • Create a new WebUserControls web project WebUserControls

  • Add a web form control and name it UserControl1

     <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="WebUserControls.UserControl1" %> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 

    and in the code for the addition:

     public string Text { get { return TextBox1.Text; } set { TextBox1.Text = value; } } 
  • In the user project, add a link to the project containing the ascx user element.

  • Copy the .ascx control .ascx to the consumer project.

    Note. You do not need to add the file to the project, but the physical file must exist in the path that you used as Src when registering the tag.

  • On the page you want to use the control, add the following:

     <%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %> 
  • Use the control this way:

     <uc:UserControl1 runat="server" Text="UserControl1" /> 

Note

  • If you do not want to copy the ascx file, you can use Web Forms Server Control, which does not rely on ascx files. Then, to use these custom controls, just register them:

     <%@ Register Assembly="WebUserControls" Namespace="WebUserControls" TagPrefix="uc" %> 
  • This answer builds on Scott Guthrie's excellent post on this topic: Creating and Using User Management Libraries
+3
source share

All Articles