Solved

client validation on click of checkbox in a nested repeater

Posted on 2008-10-30
6
3,231 Views
Last Modified: 2013-11-11
I have a repeater which shows current month records from database based upon month and year selection from dropdownlist at the top of page. This repeater has 3 textboxes per row  (date of month) and a nested repeater. Nested repeater contains checkbox and is populated from database. I want to make sure using javascript and custom validators that at least one checkbox in each row should be selected.
0
Comment
Question by:needo_jee
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22843548
Hello,
Looking from your description, you seem to have
for each outer repeater item,
  you hav 3 textboxes  and 1 child repeater  (which further down contains 1 checkbox).

But this means you only have 1 checkbox per item.
And you want to ensure all of these are selected?
I guess I'm missing something out here.
0
 
LVL 12

Author Comment

by:needo_jee
ID: 22847693
In child repeater I have list of checkboxes and one of those checkboxes should be checked.
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22851252
Hello,
Got it, I have created a sample javascript for you, just paste it as a .html file and run locally and try pressing the submit button without selecting checkboxes in each row, it will give error and tell you that you have to select these many checkboxes atleast i.e. once per each row.

This is html and javascript , but looking at the code and html I have produced, you can easily port it in your repeater, just make sure you keep ids properly and change variables accordingly in this script.

Hope it solved your problem.

Happy Javascripting!

<html>

<body>
 

<form id="form1" >

<div id="_main" style="width:100px;">
 

<div id="div1" style="background-color:Gray;">

	<input type="CHECKBOX" id="chk_1" value="This...">

	<input type="CHECKBOX" id="chk_2" value="This...">

	<input type="CHECKBOX" id="chk_3" value="This...">

</div>

Group 2
 

<div id="div2" style="background-color:Gray;">

	<input type="CHECKBOX" id="chk_1" value="This...">

	<input type="CHECKBOX" id="chk_2" value="This...">

	<input type="CHECKBOX" id="chk_3" value="This...">

</div>
 
 

<input type="hidden" id="hdn1" value="div1,div2">

</div>
 

<input type="submit" value="submit" onclick="checkform()" >

</form>

</body>

</html>
 

<script type="text/javascript">
 

function checkform()

{

  var v_main  = document.getElementById("_main");

  var v_sub = v_main.getElementsByTagName('div');

  

  var i=0;

  var faultCounter=0;

  while(i<v_sub.length)

  {	

	var v_sub2 = v_sub[i].getElementsByTagName('input');

	var innerChecked=0;

        j=0;

	while(j<v_sub2.length)

	{

		if(v_sub2[j].checked==true)

		{

			innerChecked=1;

		}	

		j++;

	}

	

	if(innerChecked==0) //i.e. no checkbox is checked

	{

	  faultCounter = faultCounter + 1;

	}

	i++;

  }
 

  if (faultCounter>0)

  {

    alert("Atleast select one checkbox frome each row, " + faultCounter + " checkbox needs to be selected yet.");

  }

  else

  {

	alert("All O.K.");

  }

}
 
 

</script>

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:techExtreme
ID: 22851307
Also forgot to mention, after you get familiar with the script I posted above,
you will notice tht
there should be one single outer div element,

then elements like div1, div2 ... can probably be once per itemtemplate of repeater,
i.e. if you have two dataitems, you should render div1,div2...  , if you have three dataitems, your repeater shoudl produce div1,div2,div3 like that.

Now inside of these divs (div1, div2... ) you can have your checkboxes.
ids of checkboxes doesn't matter as of now.


You can use this logic with two repeaters, or 1 repeater,
its just how you arrange the divs and how they render dynamically!
0
 
LVL 12

Author Comment

by:needo_jee
ID: 22851970
Hi techExtreme,

    Thanks for your time. I pasted above code in html file but when I click one checkbox from one row and dont check any from other row It prompts me with alert and then the checkbox I checked also looses selection. Why so ?

Regards,
Asif Hameed
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22852551
Its just html buddy. Once you prepare it with dynamic code and in aspx page , the session will be maintained there.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now