【问题标题】:Is It Possible To Have Custom Generic Controls in ASP.NET?是否可以在 ASP.NET 中使用自定义通用控件?
【发布时间】:2017-02-24 00:17:17
【问题描述】:

我刚刚开始使用 ASP.NET 中的自定义控件,并想尝试使用它来为对象列表创建一个表。

这是我的自定义控件:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Jordan.Custom.CS.Controls
{

    [DefaultProperty("Text"), ToolboxData("<{0}:ObjectTable runat=\"server\"> </{0}:ObjectTable>")]
    public class ObjectTable<T> : WebControl
    {
        public ObjectTable() { }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        public string Text
        {
            get
            {
                string s = (string)ViewState["Text"];
                return ((string.IsNullOrEmpty(s)) ? "[" + this.ID + "]" : s);
            }
            set
            {
                ViewState["Text"] = value;
            }
        }
        [Bindable(true), Category("Data"), DefaultValue(""), Localizable(true)]
        public List<T> ObjectValue
        {
            get { return (List<T>)ViewState["ObjectValue"]; }
            set { ViewState["ObjectValue"] = value; }
        }
        [Bindable(true), Category("Appearance"), DefaultValue(""), Localizable(true)]
        public string Heading
        {
            get { return ViewState["heading"].ToString(); }
            set { ViewState["Heading"] = value; }
        }
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center;color:red;");
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write("<h1>"+this.Heading+"</h1>");
            writer.Write("<br />");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag("<tr>");
            foreach (PropertyInfo prop in typeof(T).GetProperties()) //Pulls all the properties from the generic object
            {
                writer.RenderBeginTag("<th>");
                writer.Write(prop.Name);
                writer.RenderEndTag(); // th
            }
            writer.RenderEndTag(); // tr
            writer.RenderEndTag(); // table
        }
    }
}

但是当我尝试在页面上使用我的自定义控件时,它不起作用。它没有做我期望它做的事情,而是给出了这个:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="Jordan" Namespace="Jordan.Custom.CS.Controls" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <Jordan:ObjectTable`1 />
            </div>
        </form>
    </body>
</html>

任何帮助将不胜感激。我想知道是否可以在自定义控件中使用泛型,如果可以,如何解决我的问题?

【问题讨论】:

    标签: c# asp.net custom-controls


    【解决方案1】:

    对于将控件放在页面上的自定义控件,我认为您所做的事情是不可能的。所以

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
            <title></title>
        </head>
        <body>
            <form id="form1" runat="server">
                <div>
                    <Jordan:ObjectTable`1 />
                </div>
            </form>
        </body> </html>
    

    不知道如何传递填充的数据对象。在标记中,您只能访问字符串类型属性。所以你可以做的是定义你试图绑定的类型名,这样你就可以使用反射来加载类型并像你当前正在做的那样旋转所有属性。但是您仍然需要设置类似 DataSource 的内容并使用数据绑定来绑定到 Enumerable 对象。

    在 SO 上查看这个相关的question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多