Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I need alert the user when the check box is checked or not.
I have used check box inside the repeater.


I have tried like this..

 <script type="text/javascript">
      function validate() {
          var count = 0;
          var repeater = document.getElementById("check");
          var checkBoxes = repeater.getElementsByTagName("input");
          for (var i = 0; i <checkBoxes.length; i++)
           {
              if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) 
              {
                  count++;
              }
            
             
          }
          if (count == 0) {
              alert("Kindly select the feedback to delete");
              return false;
          }
          else {
              confirm("Are you sure you want to delete?");
          }
      }
</script
>



XML
<asp:Repeater ID="feedback" runat="server"   onitemcommand="RepterDetails_ItemCommand">
<ItemTemplate>
<table style="border-top:1px dotted #3b5998; width:650px" id="check">
<tr><td>

<asp:CheckBox ID="chk" runat="server" EnableViewState="true" />
</td>
</tr>
</table>


</ItemTemplate>
<FooterTemplate>
<tr>
<td>
    <table width="650px" style="background-color:White;height:10px">

    <td align="center">
    <asp:Button ID="lnkDelSelected" ForeColor="white" runat="server" Text="Delete Selected" onclick="LinkButton1_Click" OnClientClick="return (validate());"   CssClass="item_button"></asp:Button>
    </td></tr>
    </Table>
    </td>
    </tr>
    </FooterTemplate>
</asp:Repeater>




but its not working correctly.

if the first check box is checked in repeater, its showing correct alert message.
if the first and any other checkbox is checked in repeater, its showing correct alert message


the problem is,

if i have select remaining checkboxes, without selecting first check box,

its not taking that condition checkbox.checked



kindly suggest me the condition.


thanks in advance
Posted
Updated 15-Jul-14 3:15am
v4

How could it possibly work? You are not even trying to handle the event of changing the checked state of the check box. You can handle the event onclick of this element, and, in the event handler, checkup the state. Please see:
http://www.w3schools.com/jsref/event_onclick.asp[^],
http://www.w3schools.com/jsref/prop_checkbox_checked.asp[^].

—SA
 
Share this answer
 
Please try following solution and let me know if it works :
I don't understand why your code does not work. It seems correct.
[Edit] :
Here I have made one solution and it's working perfect :

Repeater with footer button :
ASP.NET
<asp:Repeater ID="rptUsers" runat="server">
               <HeaderTemplate>
                   <table id="check" border="1">
                       <tr>
                           <th>Select</th>
                           <th>Name
                           </th>
                           <th>Country
                           </th>
                       </tr>
               </HeaderTemplate>
               <ItemTemplate>
                   <tr>
                       <td>
                           <asp:CheckBox ID="chkbox" Text="" runat="server" />
                       </td>
                       <td>
                           <%#Eval("Name") %>
                       </td>
                       <td>
                           <%#Eval("Country") %>
                       </td>
                   </tr>
               </ItemTemplate>
               <FooterTemplate>
                   <tr>
                       <td>
                           <table style="background-color: yellow; height: 10px">
                               <tr>
                                   <td>
                                       <asp:Button ID="btn" ForeColor="Green" runat="server" OnClick="btn_Click" Text="Delete Selected" OnClientClick="return (Validate());"></asp:Button>
                                   </td>
                               </tr>
                           </table>
                       </td>
                   </tr>
                   </table>
               </FooterTemplate>
           </asp:Repeater>


Code behind to bind repeater and button click :
C#
protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             //bind repeater with dummy data
             rptUsers.DataSource = BindGrid();
             rptUsers.DataBind();
         }
     }

     //btn click example
     protected void btn_Click(object sender, EventArgs e)
     {
         Response.Write("Post back done");
     }


     //Dummy data to bind repeater
     private DataTable BindGrid()
     {
         DataTable dt = new DataTable();
         dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                     new DataColumn("Name", typeof(string)),
                     new DataColumn("Country",typeof(string)) });
         dt.Rows.Add(1, "Rajesh Biswas", "India");
         dt.Rows.Add(2, "Johny Depp", "US");
         dt.Rows.Add(3, "Mad Hatter", "US");
         dt.Rows.Add(4, "Willy Bonka", "US");
         return dt;
     }


Javascript code :

JavaScript
<script type="text/javascript">
    function Validate() {
        var count = 0;
        var repeater = document.getElementById("check");
        var checkBoxes = repeater.getElementsByTagName("input");
        for (var i = 0; i < checkBoxes.length; i++) {
            if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) {
                count++;
            }
        }
        if (count == 0) {
            alert("Kindly select the feedback to delete");
            return false;
        }
        else {
            return confirm("Are you sure you want to delete?");
        }

        //Using Jquery
        //var chkboxrowcount = $("#tblContacts input[id*='chkbox']:checkbox:checked").size();
        //if (chkboxrowcount == 0) {
        //    alert("Please select atleast one");
        //    return false;
        //}
    }
</script>



Good luck.
 
Share this answer
 
v3
Comments
priya dharshan 15-Jul-14 7:16am    
Hi Sir,
Again im getting same problem

if the first check box is checked in repeater, its showing correct alert message.
if the first and any other checkbox is checked in repeater, its showing correct alert message


the problem is,

if i have select remaining checkboxes, without selecting first check box,

its not taking that condition checkbox.checked
Raje_ 15-Jul-14 7:35am    
Are you sure you used exact code which is provided in solution. Because in my case it's working great. Can you please put breakpoint and check in which line it's breaking. :)
priya dharshan 15-Jul-14 7:41am    
i have checked sir,
its not increment count value when we checked other than first checkbox
Raje_ 15-Jul-14 7:46am    
Can you please post your modified code once again including repeater?
And please don't call me sir, I hate that. :)
priya dharshan 15-Jul-14 7:54am    
I hav updated the script..
kindly check it out

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