The check boxes rener fine but after a postback none of the Items come back as selected even thou I selected some....they simply just go back to unchecked and hence I can never tell which ones were selected. Why is this happening?
What is happening is that there isn't a uniqueID as well as a ClientID, the code below is what I used to create a custom formmatted checkbox with a label wrapped around it. (This is the format that Twitter bootstrap uses, that's why I used
it).
Dim item = MyBase.Items(repeatIndex)
Dim title = item.Attributes("title")
writer.Write(String.Format("<label title=""{0}"" class=""checkbox"">", title))
writer.WriteBeginTag("input")
writer.WriteAttribute("type", "checkbox")
writer.WriteAttribute("name", UniqueID + Me.IdSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo))
writer.WriteAttribute("id", ClientID + Me.ClientIDSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo))
writer.WriteAttribute("value", item.Value)
If item.Selected Then
writer.WriteAttribute("checked", "checked")
End If
Dim attrs As System.Web.UI.AttributeCollection = item.Attributes
For Each key As String In attrs.Keys
writer.WriteAttribute(key, attrs(key))
Next
writer.Write(">")
writer.Write(item.Text)
writer.WriteEndTag("input")
writer.WriteEndTag("label")
e.zer0
Member
4 Points
66 Posts
Overriding RenderItem() for CheckBoxList is not working properly
Aug 21, 2008 08:39 PM|LINK
This is the code I have:
public class MyCheckBoxList : CheckBoxList { #region Methods public void BindCollection(object o, string dataTextField, string dataValueField) { try { if (this.DataSource != null) this.DataSource = null; this.DataSource = o; this.DataTextField = dataTextField; this.DataValueField = dataValueField; this.DataBind(); } catch { this.DataSource = null; } } protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer) { CheckBox chk = new CheckBox(); chk.Page = this.Page; chk.ID = this.ClientID + "_" + repeatIndex.ToString(); chk.Text = this.Items[repeatIndex].Text; chk.Attributes["value"] = this.Items[repeatIndex].Value; chk.Checked = this.Items[repeatIndex].Selected; chk.CssClass = CssClass; chk.TextAlign = this.TextAlign; chk.RenderControl(writer); } #endregion } }dannylloyd
Member
2 Points
2 Posts
Re: Overriding RenderItem() for CheckBoxList is not working properly
Jan 08, 2013 03:16 PM|LINK
I'm sure this user is long gone, but for anyone who stumbles upon this, as I did, here is the solution I used.
http://sebastienlachance.com/blog/revisited-going-back-to-aspnet-webforms-and-a-lot-of-pain-with-the-checkboxlist
What is happening is that there isn't a uniqueID as well as a ClientID, the code below is what I used to create a custom formmatted checkbox with a label wrapped around it. (This is the format that Twitter bootstrap uses, that's why I used it).
Dim item = MyBase.Items(repeatIndex) Dim title = item.Attributes("title") writer.Write(String.Format("<label title=""{0}"" class=""checkbox"">", title)) writer.WriteBeginTag("input") writer.WriteAttribute("type", "checkbox") writer.WriteAttribute("name", UniqueID + Me.IdSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)) writer.WriteAttribute("id", ClientID + Me.ClientIDSeparator + repeatIndex.ToString(NumberFormatInfo.InvariantInfo)) writer.WriteAttribute("value", item.Value) If item.Selected Then writer.WriteAttribute("checked", "checked") End If Dim attrs As System.Web.UI.AttributeCollection = item.Attributes For Each key As String In attrs.Keys writer.WriteAttribute(key, attrs(key)) Next writer.Write(">") writer.Write(item.Text) writer.WriteEndTag("input") writer.WriteEndTag("label")