如何解决在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>
就是这样。那是我所能得到的。我根本无法从后面的代码访问它。好像它不存在。我需要能够用项目,触发事件等填充它。所有正常下拉列表所能做的事情。
标记似乎认为它可以发生事件:
所以我没有获得正常的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的引用:
在我的页面上注册:
<%@ 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。