Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web User Controls in different Project

I put some self made Web User Controls in a seperate Project "WebControls" and now 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 Behind:

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 bind it into a Webpage in the other project like that:

<%@ 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 now start the page it throws a Reference Null Exception in following line:

set { textBox.Width = value; }

becuase textBox = NULL

Seems my Control is not properly initiated. What am I doing wrong? How can I fix that?

like image 828
Ephedra Avatar asked Sep 21 '16 07:09

Ephedra


People also ask

What are user controls in website?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

Is it possible for us to run Web user control on your own?

As already discussed in the preceding, a User Control does not run directly on its own. To render a User Control you must use it in an . aspx page, now let us add the User Control in the . aspx page.

What is difference between page and Web user control?

ASP.NET Page can be viewed directly in the Browser. User Control cannot be viewed directly in the browser. User Controls are added to WebPages and you view them by requesting a web page in your browser. User Control does not have a HTML, Body or Form element.


1 Answers

If you want to reuse a ascx user control across multiple projects, you should copy ascx file to the consumer project and register the tag this way:

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

As an example, you can follow these steps:

  1. Create a new web project name it WebUserControls

  2. Add a Web Forms User 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 code behind add:

    public string Text
    {
        get { return TextBox1.Text; }
        set { TextBox1.Text = value; }
    }
    
  3. In the consumer project, add a reference to the project containing the ascx user control.

  4. Copy .ascx file of control into the consumer project.

    Note: You don't need to add the file to the project, but the physical file should exist in the path which you used as Src in registerting the tag.

  5. In the page which you want to use the control, add this:

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

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

Note

  • If you want to not copy ascx file, you can use Web Forms Server Control which doesn't rely on ascx files. Then for using those custom controls, it's enough to register them:

    <%@ Register Assembly="WebUserControls" Namespace="WebUserControls" TagPrefix="uc" %>
    
  • This answer relies on a great post by Scott Guthrie in this topic: Creating and Using User Control Libraries
like image 50
Reza Aghaei Avatar answered Sep 20 '22 03:09

Reza Aghaei