CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Repeater Control

    I have a repeater Control, within which I have placed checkboxes inside the item template. The No. of checkboxes depends on the number of records present in the database table.

    Now I have a task in which I have to check those checkboxes which reside inside the repeater, their checked value depends on the bit value picked from the database. So how will I do that?? My motive is to set their check state while they are loaded inside a repeater during the databound.

    Thanks in advance
    Last edited by maverick786us; July 18th, 2009 at 08:12 AM.

  2. #2
    Join Date
    Feb 2009
    Location
    Atlanta, GA
    Posts
    17

    Re: Repeater Control

    I have done something similar:


    Code:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucAllergyGroup.ascx.cs" Inherits="OnTheRoadMd.Web.ctrls.ucAllergyGroup" %>
    <label><span id="spanAllergyTitles" runat="server" enableviewstate="false" class="GroupTitles">Title</span><a id="anchorViewList" runat="server" enableviewstate="false" class="GroupTitles">{view list}</a></label>
    <div id="divAllergy" runat="server" enableviewstate="false">
    	<asp:repeater id="rptAllergy" runat="server" EnableViewState="false"
    		onitemdatabound="rptAllergy_ItemDataBound">
    		<ItemTemplate>
            <asp:CheckBox ID="CheckBoxAllergy" runat="server" /> 
            <asp:Label ID="lblAllergy" runat="server" AssociatedControlID="CheckBoxAllergy" CssClass="labelInlineBlock"></asp:Label> 
    		</ItemTemplate>
    		<SeparatorTemplate><br /></SeparatorTemplate>
    	</asp:repeater>			
    </div>
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    namespace OnTheRoadMd.Web.ctrls
    {
      public partial class ucAllergyGroup : System.Web.UI.UserControl
      {
        /* -------------------------------------------------------------------- */
        public DataTable BindingSource { get; set; }
        /* -------------------------------------------------------------------- */
        protected void Page_Load(object sender, EventArgs e)
        {
          if (this.BindingSource == null) //keep the designer from crashing
            return;
    
    
          spanAllergyTitles.InnerText = Convert.ToString(this.BindingSource.Rows[0]["CategoryName"]);
    
          anchorViewList.HRef = string.Format("javascript:showElements('{0}');", divAllergy.ClientID);
    
          if (HasItemSelected())
          {
            divAllergy.Attributes.Add("style", "display:block;");
          }
          else
          {
            divAllergy.Attributes.Add("style", "display:none;");
          }
    
          rptAllergy.DataSource = this.BindingSource;
          rptAllergy.DataBind();
        }
        /* -------------------------------------------------------------------- */
        public override void Dispose()
        {
          try
          {
            if (this.BindingSource != null)
            {
              this.BindingSource.Dispose();
              this.BindingSource = null;
            }
          }
          catch { }
          base.Dispose();
        }
        /* -------------------------------------------------------------------- */
        protected void rptAllergy_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
          DataRowView row = (e.Item.DataItem as DataRowView);
          if (row == null) //keep the designer from crashing
            return;
    
          Label lbl = e.Item.FindControl("lblAllergy") as Label;
          CheckBox cbAllergy = e.Item.FindControl("CheckBoxAllergy") as CheckBox;
          lbl.Text = Server.HtmlEncode(Convert.ToString(row["AllergyName"]));
    
          if (!IsPostBack)
            cbAllergy.Checked = Convert.ToBoolean(row["Checked"]);
        }
        /* -------------------------------------------------------------------- */
        private bool HasItemSelected()
        {
          foreach (DataRow row in this.BindingSource.Rows)
          {
            if (Convert.ToBoolean(row["Checked"]))
              return true;
          }
          return false;
        }
        /* -------------------------------------------------------------------- */
      }
    }
    Scott Knake
    Custom Software Development
    Apex Software, Inc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured