Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
thhis is my asp page
ASP.NET
<%@ 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>

C#
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
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

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.

C#
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();
       }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900