ASP.NET:自定义CheckBoxList控件在PostBack时丢失选中状态。
创始人
2024-09-20 00:30:46
0

这个问题可以通过保存选定状态并在PostBack后重新加载该状态来解决。 以下是实现此解决方案的示例代码:

首先,首先创建一个名为CheckedListItem的自定义控件,该控件表示CheckBoxList的每个项。该控件包括一个CheckBox和一个HiddenField,用于将其选中状态传递到服务器并在PostBack后重新加载状态。

public class CheckedListItem : System.Web.UI.WebControls.WebControl { private System.Web.UI.WebControls.CheckBox _checkBox; private System.Web.UI.WebControls.HiddenField _hiddenField;

public CheckedListItem()
{
    _checkBox = new System.Web.UI.WebControls.CheckBox();
    _hiddenField = new System.Web.UI.WebControls.HiddenField();
    Controls.Add(_checkBox);
    Controls.Add(_hiddenField);
}

public bool Checked
{
    get { return _checkBox.Checked; }
    set { _checkBox.Checked = value; }
}

public string Value
{
    get { return _hiddenField.Value; }
    set { _hiddenField.Value = value; }
}

}

然后,创建一个名为CheckedList的自定义服务器控件,该控件表示多个CheckedListItem控件。该控件还包括一个名为CheckedValues的属性,用于在PostBack后保存选定状态。

public class CheckedList : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer { private List _items = new List();

public CheckedList()
{
}

protected override void CreateChildControls()
{
    Controls.Clear();
    foreach (var item in Items)
    {
        Controls.Add(item);
    }
}

public List Items
{
    get { return _items; }
    set { _items = value; }
}

public IEnumerable CheckedValues
{
    get
    {
        EnsureChildControls();
        return Items.Where(x => x.Checked).Select(x => x.Value);
    }
    set
    {
        EnsureChildControls();
        foreach (CheckedListItem item in Items)
        {

相关内容

热门资讯

hhpoker透视脚本下载!w... hhpoker透视脚本下载!wepoker插件功能辅助器(透视)插件-确实解迷真的是有挂hhpoke...
智星菠萝辅助!hhpoker真... 智星菠萝辅助!hhpoker真的有透视吗(透视)挂-本来透视是真的挂1、起透看视 hhpoker真的...
pokemmo辅助官网!wep... pokemmo辅助官网!wepoker俱乐部辅助器(透视)脚本-其实解谜真的有挂1、起透看视 wep...
pokerworld破解版下载... pokerworld破解版下载!智星德州插件怎么下载(透视)技巧-真是揭露真的有挂智星德州插件怎么下...
hhpoker透视脚本下载!w... hhpoker透视脚本下载!wpk透视辅助方法(透视)脚本-果然必备真的有挂hhpoker透视脚本下...
悦扑克脚本!wepoker究竟... 悦扑克脚本!wepoker究竟有没有透视(透视)工具-本来科普存在有挂一、wepoker究竟有没有透...
pokemmo手机版修改器!w... pokemmo手机版修改器!wepoker辅助器官方(透视)插件-其实关于是真的挂1、实时pokem...
wepoker怎么发冤家牌!h... wepoker怎么发冤家牌!hhpoker软件可以玩吗(透视)方法-果然教你存在有挂1、超多福利:超...
拱趴大菠萝十三水透视!智星德州... 拱趴大菠萝十三水透视!智星德州可以透视吗(透视)挂-本来教你是真的挂1、下载好拱趴大菠萝十三水透视正...
pokemmo脚本最新版!we... pokemmo脚本最新版!wepoker破解器激活码(透视)教程-总是详情真的有挂1、每一步都需要思...