简体   繁体   English

使用LoadControl以编程方式加载WebControl(而非UserControl)

[英]Use LoadControl to programmatically load WebControl (not UserControl)

I have a custom WebControl class that represents a HyperLink surrounded by an li (list item) element. 我有一个自定义WebControl类,该类表示一个由li (列表项)元素包围的HyperLink I would like to be able to load this control and add it to the page programmatically. 我希望能够加载此控件并将其以编程方式添加到页面中。

Here is the custom web control ( ListItemHyperLink.cs ): 这是自定义Web控件( ListItemHyperLink.cs ):

public class ListItemHyperLink : HyperLink
{
    public string ListItemCssClass { get; set; }

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        if (!String.IsNullOrWhiteSpace(ListItemCssClass))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, ListItemCssClass);
        }
        writer.RenderBeginTag("li");
        base.RenderBeginTag(writer);
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);
        writer.RenderEndTag();
    }
}

Here is the page I am attempting to add it to ( Test.aspx ): 这是我尝试将其添加到( Test.aspx )的页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Example.Test" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>My Page</title>
</head>
<body>
    <form id="Form1" runat="server">
        <ul runat="server" ID="UnorderedList">
        </ul>
    </form>
</body>
</html>

And here is my attempt to load the control dynamically in the code-behind of the page ( Test.aspx.cs ): 这是我尝试在页面代码( Test.aspx.cs )中动态加载控件:

public partial class Test : Page
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var listItemHyperLink = (ListItemHyperLink) LoadControl("~/Controls/ListItemHyperLink.cs");
        listItemHyperLink.Text = "Google";
        listItemHyperLink.NavigateUrl = "http://google.com/";
        listItemHyperLink.ListItemCssClass = "li-class";
        listItemHyperLink.CssClass = "a-class";
        UnorderedList.Controls.Add(listItemHyperLink);
    }
}

When I try this, I get the following Exception: 尝试此操作时,出现以下异常:

Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Util.IWebObjectFactory'. 无法将类型为“ System.Web.Compilation.BuildResultCompiledAssembly”的对象转换为类型为“ System.Web.Util.IWebObjectFactory”的对象。

This exception is thrown on the line where I call LoadControl() . 我调用LoadControl()的行上引发了此异常。

How can I use LoadControl() (or some other method) to use this control from code? 如何使用LoadControl() (或其他方法)从代码中使用此控件?

Just enstantiate your custom control and add it to the Controls collection. 只需实例化自定义控件并将其添加到Controls集合中即可。 LoadControl is for UserControls. LoadControl用于UserControls。

var listItemHyperLink = new ListItemHyperLink();
listItemHyperLink.Text = "Google";
listItemHyperLink.NavigateUrl = "http://google.com/";
listItemHyperLink.ListItemCssClass = "li-class";
listItemHyperLink.CssClass = "a-class";
UnorderedList.Controls.Add(listItemHyperLink);

ListItemHyperLink is a server control, so you cannot load it like User control. ListItemHyperLink是一个服务器控件,因此无法像用户控件一样加载它。

protected void Page_Load(object sender, EventArgs e)
{
    var listItemHyperLink = new ListItemHyperLink();
    listItemHyperLink.Text = "Google";
    listItemHyperLink.NavigateUrl = "http://google.com/";
    listItemHyperLink.ListItemCssClass = "li-class";
    listItemHyperLink.CssClass = "a-class";
    UnorderedList.Controls.Add(listItemHyperLink);
}

It is kind of odd to see PageLoad using override method. 看到使用重写方法的PageLoad有点奇怪。 It is not normal unless you have a reason behind it. 除非您有其背后的原因,否则这是不正常的。 It is like using Int32 instead of int. 就像使用Int32而不是int一样。 I'm just saying; 我只是说; I hope you don't mind. 我希望你不要介意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM