ASP.NET关于继承DropDownList的自定义DDL控件
public class DDL : DropDownList
public DDL()
{
if (!this.DesignMode)
DataBind();
}
{
if (!this.DesignMode)
DataBind();
}
public override void DataBind()
{
if (List<ML_DDL> != null)
{
Items.Clear();
foreach (ML_DDL item in List<ML_DDL>)
{
ListItem temp = new ListItem(item.Name, item.Code);
Items.Add(temp);
}
}
}
{
if (List<ML_DDL> != null)
{
Items.Clear();
foreach (ML_DDL item in List<ML_DDL>)
{
ListItem temp = new ListItem(item.Name, item.Code);
Items.Add(temp);
}
}
}
当对DDL的下拉项进行编辑的时候(ListItem集合编辑器)、或设置AutoPostBack的值为true,会自动进行数据添加进去……即便是对当前状态DesignMode进行了判定
<cc1:DDL ID="DDL1" runat="server" AutoPostBack="True">
……
<asp:ListItem Value="Code">Name</asp:ListItem>
……
</cc1:DDL>
</cc1:DDL>
这是为什么呢?如果在重写的Render方法内再对数据绑定,页面上又见不到绑定信息……而且可能导致使用时候设置不到绑定的值
protected override void Render(System.Web.UI.HtmlTextWriter writer)
什么才是更好的办法呢?……
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步