微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ASP.NET Webform中扩展DropDownList

如何解决在ASP.NET Webform中扩展DropDownList

我正在尝试创建自己的下拉列表以在网页上使用,但我无法按照我需要的方式工作。

以最基本的形式,我可以创建一个类,从System.Web.UI.WebControls.DropDownList继承,在页面注册它,添加标记,它至少会在页面显示一个控件:

我的课:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomDropdown
    <DefaultProperty("Text"),ToolBoxData("<{0}:DropDownListWithAttributes runat=server></{0}:DropDownListWithAttributes>")>
    Public Class DropDownListWithAttributes
        Inherits System.Web.UI.WebControls.DropDownList

    End Class
End Namespace

页面注册

<%@ Register TagPrefix="myControls" Namespace="CustomDropdown" %>

标记

<myControls:DropDownListWithAttributes ID="ddlMyTestCustomDDL" runat="server"></myControls:DropDownListWithAttributes>

enter image description here

就是这样。那是我所能得到的。我根本无法从后面的代码访问它。好像它不存在。我需要能够用项目,触发事件等填充它。所有正常下拉列表所能做的事情。

标记似乎认为它可以发生事件:

enter image description here

但是当我添加一个时,设计器就会中断:

enter image description here

所以我没有获得正常的ddl功能,我无法从后面的代码访问控件,添加任何类型的事件中断内容...我对如何使此工作不知所措:(>

解决方法

嗯,在安德鲁的建议下并将其移至自己的项目中,它现在似乎正在工作。看起来是这样的:

C#类库:具有单个.cs文件的DropDownWithAttributes:DropDownListWithAttributes.cs

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: TagPrefix("DropDownWithAttributes","myControl")]
namespace DropDownWithAttributes
{
    [AspNetHostingPermission(SecurityAction.Demand,Level = AspNetHostingPermissionLevel.Minimal),AspNetHostingPermission(SecurityAction.InheritanceDemand,Level = AspNetHostingPermissionLevel.Minimal)]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class DropDownListWithAttributes : DropDownList
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        protected override object SaveViewState()
        {
            // create object array for Item count + 1
            object[] allStates = new object[this.Items.Count + 1];

            // the +1 is to hold the base info
            object baseState = base.SaveViewState();
            allStates[0] = baseState;

            Int32 i = 1;
            // now loop through and save each Style attribute for the List
            foreach (ListItem li in this.Items)
            {
                Int32 j = 0;
                string[][] attributes = new string[li.Attributes.Count][];
                foreach (string attribute in li.Attributes.Keys)
                {
                    attributes[j++] = new string[] { attribute,li.Attributes[attribute] };
                }
                allStates[i++] = attributes;
            }
            return allStates;
        }

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] myState = (object[])savedState;

                // restore base first
                if (myState[0] != null)
                    base.LoadViewState(myState[0]);

                Int32 i = 1;
                foreach (ListItem li in this.Items)
                {
                    // loop through and restore each style attribute
                    foreach (string[] attribute in (string[][])myState[i++])
                    {
                        li.Attributes[attribute[0]] = attribute[1];
                    }
                }
            }
        }
    }
}

我将项目添加到解决方案中,并从Web项目中添加了对DropDownWithAttributes的引用:

enter image description here

在我的页面上注册:

<%@ Register TagPrefix="myControl" Namespace="DropDownWithAttributes" Assembly="DropDownWithAttributes" %>

在页面上添加了控件:

<myControl:DropDownListWithAttributes ID="ddlWithAttribute" runat="server" OnSelectedIndexChanged="ddlWithAttribute_SelectedIndexChanged"></myControl:DropDownListWithAttributes>

那是以前发生问题的地方。...现在看起来好多了:

 '''<summary>
    '''ddlWithAttribute control.
    '''</summary>
    '''<remarks>
    '''Auto-generated field.
    '''To modify move field declaration from designer file to code-behind file.
    '''</remarks>
    Protected WithEvents ddlWithAttribute As Global.DropDownWithAttributes.DropDownListWithAttributes

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?