Click here to Skip to main content
12,947,379 members (49,073 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,
thhis is my asp page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" >
            <ItemTemplate>
                <div>
                    <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("val") %>' />
                </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button Text="Click" OnClick="Button2_Click" runat="server" />
    </form>
</body>
</html>

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("val", typeof(string));
        for (int i = 0; i < 10; i++)
            dt.Rows.Add("testing" + i.ToString());
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string Rpt = "Repeater Items Checked:<br />";
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
            if (chk.Checked)
            {
                Rpt += (chk.Text + "<br />");
            }
        }
        Response.Write(Rpt);
    }
}


i want to get the value of selected check boxes, but it doesn't work!!!!!!
Posted 26-Jan-13 2:35am
Comments
Sandeep Mewara 26-Jan-13 12:43pm
   
Code looks fine. What do you see when you debug? Not running as expected?
How do you show associated data as checkbox?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I assume you are a beginner
when you press click button it fires postback event and call Page_Load event, which rebind the repeater and calls Button2_Click therefore no items is checked use the following in page load method.

if (!IsPostBack)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("val", typeof(string));
           for (int i = 0; i < 10; i++)
               dt.Rows.Add("testing" + i.ToString());
           Repeater1.DataSource = dt;
           Repeater1.DataBind();
       }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,059
CHill60 3,235
Maciej Los 2,538
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 15 Jan 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100