Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASM checkbox
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 at 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
0 OriginalGriff 675
1 CPallini 320
2 Sergey Alexandrovich Kryukov 305
3 BillWoodruff 285
4 Peter Leow 255


Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
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