ASP.NET:自定义CheckBoxList控件在回发时丢失检查状态



我扩展了ASP.NET CheckBoxList web控件以创建Bootstrap 5布局版本。

该控件工作正常,但在回发时,它将失去选中状态。此外,控件的SelectedItem属性为null。

我为RadioButtonList创建了相同的控件,它运行得很好。使用DotPeek,我看到这两者继承了相同的控件和接口,所以我不明白为什么自定义RadioButtonList保持状态,而CheckboxList不保持状态。

有什么想法吗?互联网上没有可用的例子。

C#

public class Bootstrap5CheckBoxList : CheckBoxList {
protected override void Render(HtmlTextWriter writer) {
try {
var selected = false;
//var webControl = new WebControl(HtmlTextWriterTag.Div);
//webControl.ID = ClientID;
//webControl.RenderBeginTag(writer);
for (int index = 0; index < Items.Count; index++) {
var item = this.Items[index];
//div
writer.Indent++;
writer.WriteBeginTag($"div class='form-check {base.CssClass}'");
writer.Write('>');
writer.WriteLine();
//input
writer.Indent++;
writer.WriteBeginTag("input");
writer.WriteAttribute("id", $"{this.ID}_{index}");
writer.WriteAttribute("type", "checkbox");
writer.WriteAttribute("name", $"{this.UniqueID}_{index}");
var cssClass = "";
if (item.Attributes["class"] != null) {
cssClass = item.Attributes["class"];
}
writer.WriteAttribute("class", $"form-check-input {cssClass}");
writer.WriteAttribute("value", item.Value);
var clientID = this.ClientID;
if (item.Selected) {
if (selected) {
this.VerifyMultiSelect();
}
selected = true;
writer.WriteAttribute("checked", "checked");
}
if (item.Attributes.Count > 0) {
foreach (string key in item.Attributes.Keys) {
if (!"class".Equals(key)) {
writer.WriteAttribute(key, item.Attributes[key]);
}
}
}

if (!item.Enabled)
writer.WriteAttribute("disabled", "disabled");
if (this.Page != null) {
this.Page.ClientScript.RegisterForEventValidation(
this.UniqueID,
item.Value);
}
writer.Write('>');
writer.WriteEndTag("input");
writer.WriteLine();
//label
writer.WriteBeginTag("label");
writer.WriteAttribute("class", "form-check-label");
writer.WriteAttribute("for", $"{this.ID}_{index}");
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("label");
writer.Indent--;
writer.WriteLine();
//Close Div
writer.WriteEndTag("div");
writer.WriteLine();
writer.Indent--;

}
//webControl.RenderEndTag(writer);
} catch (Exception ex) {
throw new Exception(string.Format("{0}.{1}:{2} {3}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, ex.StackTrace));
}
}

}

HTML

<%@ Register TagPrefix="BSControls" Namespace="My.App.classes.custom_controls" Assembly="My.App" %>
<BSControls:Bootstrap5CheckBoxList ID="customCheckList" runat="server">
<asp:ListItem Value="1">Check 1</asp:ListItem>
<asp:ListItem Value="2">Check 2</asp:ListItem>
</BSControls:Bootstrap5CheckBoxList>

看起来您现在正在使用html控件,并且它们没有自动视图状态。您最好使用复选框列表,并使用引导程序对其进行格式化。而且最好在用户控件标记中包含该复选框列表,如果可能的话,不要编写代码来注入这样的控件。

因此,普通的jane复选框(inputtype=checkbox(作为一个通用规则并不像asp.net控件那样具有自动查看状态。因此,要么将复选框列表放入用户控件标记中,要么很可能需要添加代码来保存/恢复值,因为这看起来很像是在注入";输入";控件,而不是使用asp.net复选框列表。

经过多次试验,我能够做到这一点,答案令人惊讶,甚至可能不简单。

"name"属性是键,并且必须具有正确的格式。

格式不正确

writer.WriteAttribute("name", $"{this.UniqueID}_{index}");

正确格式

writer.WriteAttribute("name", $"{this.UniqueID}${index}");

您必须使用$分隔符,而不是下划线。回发时,CheckBoxList中的LoadPostData方法遍历集合以检索检查状态。

相关内容

最新更新