c# - WebControl 中的自定义 ListItemCollection
问题描述
我正在Feature
为 SharePoint 开发一个,但我是全新的 ascx。
该类System.Web.UI.WebControls.CheckBoxList
是我需要的,但我需要自定义ListItemCollection Items
of,System.Web.UI.WebControls.ListControl
因为我需要向 ListItem 添加一个属性。
我写了以下内容:
namespace My.SharePoint.Controls
{
public class CheckBoxListWithSelectBox : CheckBoxList
{
private DialogFieldItemCollection items;
private ICustomResourcer _recourcer;
public void SetCustomResourcer(ICustomResourcer resourcer)
{
_recourcer = resourcer;
}
public new DialogFieldItemCollection Items
{
get
{
if (this.items == null)
{
this.items = new DialogFieldItemCollection();
if (base.IsTrackingViewState)
{
this.items.TrackViewState();
}
}
return this.items;
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<tr><td><img width='1' height='3' style='display: block;' alt='' src='/_layouts/15/images/blank.gif?rev=40' data-accessibility-nocheck='true'/></td></tr>");
writer.Write("<tr>");
writer.Write("<td width='11'><img width='11' height='1' style='display: block;' alt='' src='/_layouts/15/images/blank.gif?rev=40' data-accessibility-nocheck='true'/></td>");
writer.Write("<td width='99%' class='ms-authoringcontrols'><table>");
IEnumerable<DialogFieldItem> selectedItems = from i in Items.Cast<DialogFieldItem>()
where i.Selected
select i;
foreach (DialogFieldItem field in selectedItems)
{
writer.Write(BuildTr(field, field.Position));
}
IEnumerable<DialogFieldItem> unSelectedItems = from i in Items.Cast<DialogFieldItem>()
where !i.Selected
select i;
int selectedItemCount = selectedItems.Count();
for (int j = 0; j < unSelectedItems.Count(); j++)
{
writer.Write(BuildTr(unSelectedItems.ElementAt(j), selectedItemCount + j));
}
writer.Write("</table></td></tr>");
}
private string BuildTr(DialogFieldItem item, int index)
{
StringBuilder sb = new StringBuilder();
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("<input type='checkbox' ");
if (item.Selected)
{
sb.Append("checked='checked' ");
}
sb.Append($"name='ShouldDisplay{HttpUtility.HtmlEncode(item.Value)}' ");
sb.Append($"id='ShouldDisplay{HttpUtility.HtmlEncode(item.Value)}' ");
sb.Append($"onclick=''");
sb.Append("></input>");
sb.Append($"<label for='ShouldDisplay{HttpUtility.HtmlEncode(item.Value)}'>{HttpUtility.HtmlEncode(item.Text)}</label>");
sb.Append("</td>");
sb.Append("<td> </td>");
sb.Append("<td>");
sb.Append($"<select title='{_recourcer.GetString("wss", "viewedit_position_param")}' onchange='Reorder(this, {index}, {Items.Count})' name='ViewOrder{index}'>");
for (int i = 0; i < Items.Count; i++)
{
sb.Append($"<option value='{i + 1}_{HttpUtility.HtmlEncode(item.Value)}'");
if (i == index)
{
sb.Append("selected='selected' ");
}
sb.Append($">{i + 1}</option>");
}
sb.Append("</select>");
sb.Append("</td>");
sb.Append("</tr>");
return sb.ToString();
}
}
}
它有效,输出正确,但是当数据从客户端提交回服务器时,Items
变量为空。我猜数据绑定丢失了,但我不知道如何/做什么,没有在网上找到任何信息(不知道到底要寻找什么)。
解决方案
推荐阅读
- c++ - 为什么 C++ 中的派生类不能直接访问(而不是通过“this->”访问)其抽象基类的受保护成员?
- c# - 如何在集合中的重复值之间挑选值?
- java - 使用 try-catch 编写单元测试
- microsoft-graph-api - Microsoft Teams 中的消息扩展
- python - 访问neo4j数据库的多个python脚本
- php - 在 WooCommerce 中扩展 get_european_union_countries() 函数
- django - 如何获取 django 的 ManyToManyField 的输入?
- html - 如何使长标签与输入字段内联
- php - 正则表达式只允许特定国家的电话代码
- node.js - 在 Node.js 中将缓冲区转换为可读的 JSON 格式