基于RadComboBox的ASP.NET服务器控件-回发问题
我试图创建一个自定义控件,该控件扩展了Telerik中的RadComboBox,以创建带有默认模板的下拉复选框列表。计划是在几个地方使用控件,所以我想将所有逻辑整合到一个位置 然而,我在回邮时遇到了一些奇怪的问题。如果选中几个项目,然后单击应用按钮,则会选择正确的项目,但复选框上的文本不同。然后在下一次回发时,我发现了一个错误基于RadComboBox的ASP.NET服务器控件-回发问题,asp.net,controls,postback,telerik,Asp.net,Controls,Postback,Telerik,我试图创建一个自定义控件,该控件扩展了Telerik中的RadComboBox,以创建带有默认模板的下拉复选框列表。计划是在几个地方使用控件,所以我想将所有逻辑整合到一个位置 然而,我在回邮时遇到了一些奇怪的问题。如果选中几个项目,然后单击应用按钮,则会选择正确的项目,但复选框上的文本不同。然后在下一次回发时,我发现了一个错误发现了多个具有相同ID“i2”的控件。FindControl要求控件具有唯一的ID。 附加的是自定义控件。感谢您的帮助 C#代码: // ///DropdownCheck
发现了多个具有相同ID“i2”的控件。FindControl要求控件具有唯一的ID。
附加的是自定义控件。感谢您的帮助
C#代码:
//
///DropdownCheckboxList的私有标头模板类
///
类CheckboxListFooterTemplate:ITemplate
{
#区域公共方法
公共容器(控制容器)
{
字符串页脚=”;
container.Controls.Add(newliteralcontrol(footer));
}
#端域公共方法
}
///
///DropdownCheckboxList的私有标头模板类
///
类CheckboxListHeaderTemplate:ITemplate
{
#区域公共方法
公共容器(控制容器)
{
字符串标题=”;
标题+=“”;
container.Controls.Add(新的LiteralControl(string.Format(header,container.Parent.ClientID));
}
#端域公共方法
}
///
///DropdownChecklistBox的模板类
///
类CheckboxListTemplate:ITemplate
{
#区域常数
//单击listitem时,此div将阻止列表关闭
常量字符串头=”;
const string tail=“”;
#端区常数
#区域私有方法
///
///将数据绑定到复选框
///
///要将数据绑定到的复选框
///
私有无效复选框\u数据绑定(对象发送方、事件参数e)
{
复选框目标=(复选框)发件人;
RadComboBoxItem=(RadComboxItem)target.BindingContainer;
string itemText=(string)DataBinder.Eval(item,“Text”);
target.Text=itemText;
}
#端域私有方法
#区域公共方法
///
///在模板中创建复选框列表项
///
///将添加控件的容器
公共容器(控制容器)
{
复选框=新复选框();
checkbox.ID=“chkList”;
Add(“onclick”,string.Format(“onCheckBoxClick(this,\“{0}\”),container.Parent.ClientID));
container.Controls.Add(newliteralcontrol(head));
checkbox.DataBinding+=neweventhandler(checkbox\u DataBinding);
container.Controls.Add(复选框);
container.Controls.Add(newliteralcontrol(tail));
}
#端域公共方法
}
//待办事项:完整摘要
///
///基于telerik演示:http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/templates/defaultcs.aspx
///
[默认属性(“文本”)]
[ToolboxData(“”)
公共类下拉复选框列表:RadComboBox,InAdminContainer
{
#区域私有财产
字符串选择文本
{
得到
{
StringBuilder值=新建StringBuilder(SelectedItems.Count);
foreach(SelectedItems中的RadComboBoxItem项)
值。追加(item.Text+“,”);
如果(value.Length>0)
返回values.ToString().Remove(values.Length-2,2);
其他的
返回空消息;
}
}
#endregion私有财产
#区域公共财产
公共RadComboxItemCollection SelectedItems
{
得到
{
复选框chk=null;
RadComboBoxItemCollection selectedItems=新RadComboxItemCollection(此);
foreach(RadComboboxItems中的项目)
{
chk=(复选框)item.FindControl(“chkList”);
如果(chk!=null&&chk.Checked)
选择编辑项。添加(项);
}
返回selectedItems;
}
}
//待办事项:总结
公共重写字符串SelectedValue
{
得到
{
StringBuilder值=新建StringBuilder(SelectedItems.Count);
foreach(SelectedItems中的RadComboBoxItem项)
值。追加(item.Value+“,”);
如果(value.Length>0)
返回values.ToString().Remove(values.Length-2,2);
其他的
返回“”;
}
设置
{
if(值!=null)
SelectedValues=新列表(value.Split(',');
}
}
//待办事项:总结
公共列表选定值
{
得到
{
List selectedValues=新建列表();
foreach(SelectedItems中的RadComboBoxItem项)
{
选择值。添加(项。值);
}
返回所选的值;
}
设置
{
RadComboxItem=null;
复选框chk=null;
foreach(值中的字符串val)
{
item=Items.FindItemByValue(val.Trim());
如果(项!=null)
{
chk=(复选框)item.FindControl(“chkList”);
如果(chk!=null)
chk.Checked=真;
}
}
}
}
#端域公共属性
#区域保护方法
受保护的覆盖无效CreateChildControls()
{
if(base.HeaderTemplate==null)
base.HeaderTemplate=新复选框ListHeaderTemplate();
if(base.ItemTemplate==null)
base.ItemTemplate=新CheckboxListTemplate();
if(base.FooterTemplate==null)
base.FooterTemplate=新的CheckboxListFooterTemplate();
base.CreateChildControls();
}
受保护的覆盖无效OnPr
/// <summary>
/// Private Header template class for the DropdownCheckboxList
/// </summary>
class CheckboxListFooterTemplate : ITemplate
{
#region Public Methods
public void InstantiateIn(Control container)
{
string footer = "<input type=\"submit\" value=\"Apply\" />";
container.Controls.Add(new LiteralControl(footer));
}
#endregion Public Methods
}
/// <summary>
/// Private Header template class for the DropdownCheckboxList
/// </summary>
class CheckboxListHeaderTemplate : ITemplate
{
#region Public Methods
public void InstantiateIn(Control container)
{
string header = "<input type=\"button\" value=\"Check All\" onclick=\"CheckAll("{0}", true)\" />";
header += " <input type=\"button\" value=\"Uncheck All\" onclick=\"CheckAll("{0}", false)\" />";
container.Controls.Add(new LiteralControl(string.Format(header, container.Parent.ClientID)));
}
#endregion Public Methods
}
/// <summary>
/// Template class for the DropdownChecklistBox
/// </summary>
class CheckboxListTemplate : ITemplate
{
#region Constants
//this div will stop the list from closing as a listitem is clicked
const string head = "<div onclick=\"StopPropagation(event)\" class=\"combo-item-template\">";
const string tail = "</div>";
#endregion Constants
#region Private Methods
/// <summary>
/// Bind the data to the checkbox
/// </summary>
/// <param name="sender">Checkbox to bind data to</param>
/// <param name="e"></param>
private void checkbox_DataBinding(object sender, EventArgs e)
{
CheckBox target = (CheckBox)sender;
RadComboBoxItem item = (RadComboBoxItem)target.BindingContainer;
string itemText = (string)DataBinder.Eval(item, "Text");
target.Text = itemText;
}
#endregion Private Methods
#region Public Methods
/// <summary>
/// Create the checkbox list items in the template
/// </summary>
/// <param name="container">Container that the control will be added</param>
public void InstantiateIn(Control container)
{
CheckBox checkbox = new CheckBox();
checkbox.ID = "chkList";
checkbox.Attributes.Add("onclick", string.Format("onCheckBoxClick(this, \"{0}\")", container.Parent.ClientID));
container.Controls.Add(new LiteralControl(head));
checkbox.DataBinding += new EventHandler(checkbox_DataBinding);
container.Controls.Add(checkbox);
container.Controls.Add(new LiteralControl(tail));
}
#endregion Public Methods
}
//todo: complete summary
/// <summary>
/// based on telerik demo: http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/templates/defaultcs.aspx
/// </summary>
[DefaultProperty("Text")]
[ToolboxData("<{0}:DropdownCheckboxList runat=server></{0}:DropdownCheckboxList>")]
public class DropdownCheckboxList : RadComboBox, INamingContainer
{
#region Private Properties
string SelectedText
{
get
{
StringBuilder values = new StringBuilder(SelectedItems.Count);
foreach (RadComboBoxItem item in SelectedItems)
values.Append(item.Text + ", ");
if (values.Length > 0)
return values.ToString().Remove(values.Length - 2, 2);
else
return EmptyMessage;
}
}
#endregion Private Properties
#region Public Properties
public RadComboBoxItemCollection SelectedItems
{
get
{
CheckBox chk = null;
RadComboBoxItemCollection selectedItems = new RadComboBoxItemCollection(this);
foreach (RadComboBoxItem item in Items)
{
chk = (CheckBox)item.FindControl("chkList");
if (chk != null && chk.Checked)
selectedItems.Add(item);
}
return selectedItems;
}
}
//todo: summary
public override string SelectedValue
{
get
{
StringBuilder values = new StringBuilder(SelectedItems.Count);
foreach (RadComboBoxItem item in SelectedItems)
values.Append(item.Value + ", ");
if (values.Length > 0)
return values.ToString().Remove(values.Length - 2, 2);
else
return "";
}
set
{
if (value != null)
SelectedValues = new List<string>(value.Split(','));
}
}
//todo: summary
public List<string> SelectedValues
{
get
{
List<string> selectedValues = new List<string>();
foreach (RadComboBoxItem item in SelectedItems)
{
selectedValues.Add(item.Value);
}
return selectedValues;
}
set
{
RadComboBoxItem item = null;
CheckBox chk = null;
foreach (string val in value)
{
item = Items.FindItemByValue(val.Trim());
if (item != null)
{
chk = (CheckBox)item.FindControl("chkList");
if (chk != null)
chk.Checked = true;
}
}
}
}
#endregion Public Properties
#region Protected Methods
protected override void CreateChildControls()
{
if (base.HeaderTemplate == null)
base.HeaderTemplate = new CheckboxListHeaderTemplate();
if (base.ItemTemplate == null)
base.ItemTemplate = new CheckboxListTemplate();
if (base.FooterTemplate == null)
base.FooterTemplate = new CheckboxListFooterTemplate();
base.CreateChildControls();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "CustomControls.DropdownCheckboxList.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(CustomControls.DropdownCheckboxList), resourceName);
Text = SelectedText;
}
#endregion Protected Methods
}
//based on telerik demo: http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/templates/defaultcs.aspx
var cancelDropDownClosing = false;
function StopPropagation(e) {
//cancel bubbling
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
}
function onDropDownClosing() {
cancelDropDownClosing = false;
}
function CheckAll(comboBoxId, value) {
var combo = $find(comboBoxId);
//get the collection of all items
var items = combo.get_items();
//enumerate all items
for (var i = 0; i < items.get_count(); i++) {
var item = items.getItem(i);
//get the checkbox element of the current item
var chk1 = $get(combo.get_id() + "_i" + i + "_chkList");
chk1.checked = value;
}
}
function onCheckBoxClick(chk, comboBoxId) {
var combo = $find(comboBoxId);
//holds the text of all checked items
var text = "";
//holds the values of all checked items
var values = "";
//get the collection of all items
var items = combo.get_items();
//enumerate all items
for (var i = 0; i < items.get_count(); i++) {
var item = items.getItem(i);
//get the checkbox element of the current item
var chk1 = $get(combo.get_id() + "_i" + i + "_chkList");
if (chk1.checked) {
text += item.get_text() + ", ";
values += item.get_value() + ", ";
}
}
//remove the last comma from the string
text = removeLastComma(text);
values = removeLastComma(values);
if (text.length > 0) {
//set the text of the combobox
combo.set_text(text);
}
else {
//all checkboxes are unchecked
//so reset the controls
combo.set_text("");
}
}
//this method removes the ending comma from a string
function removeLastComma(str) {
return str.replace(/,$/, "");
}
chk = (CheckBox)item.FindControl("chkList");
foreach (var o in item.Controls)
{
if (o is CheckBox)
{
chk = (CheckBox) o;
}
}