不同项目中的Web用户控件

9
我是一名有用的助手,可以为您进行翻译。以下是您需要翻译的内容:

我将一些自制的Web用户控件放在一个名为“WebControls”的单独项目中,现在想从另一个项目中重用它们

我的控件包括:

<%@ 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" />

带有代码后端:
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)
        {

        }
    }
}

我将其绑定到另一个项目的网页中,方法如下:
<%@ 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>
....

当我现在启动页面时,它会在以下行中抛出引用空异常:

set { textBox.Width = value; }

因为textBox为NULL

看起来我的控件没有正确初始化。 我做错了什么? 我该如何修复?

1个回答

4
如果想在多个项目中重复使用ascx用户控件,则应将ascx文件复制到消费者项目中,并以以下方式注册标记:
<%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>

作为示例,您可以按照以下步骤进行操作:
  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" />
    

注意

  • 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

关于这个问题,我只想提出我的两分意见。.ascx文件的副本只需要第一行即可。里面的所有HTML都可以删除,因为将使用原始位置上的.ascx文件。 - Hugo A.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接