Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through GridView verifying checkbox has been checked

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am using VS 2005.
In my ASP.NET page, I wrote a CustomValidator that calls a ClientSide Javascript function.
How do I loop through my GridView checking to make sure at least one checkbox has been checked?
Here is some code:
Code:
<asp:GridView ID="GridView1" ...

<asp:CustomValidator ID="CVGridView1" 
                        OnServerValidate="CVGridView1_ServerValidate"        
                        ClientValidationFunction="CVGridView1_ClientValidate"
                        SetFocusOnError="true"
                        Text="*" 
                        runat="server"
                        ErrorMessage="Distribution Type must be selected" />

function CVGridView1_ClientValidate()
{
//How to loop through GridView and check if at least one checkbox has been checked
}
 
I'm sure I could answer your question if I knew anything about ASP.Net.... Why not post your markup so that those of us that don't know ASP.Net don't have to guess what it looks like.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
You can do this:

Code:
var gridViewObj = document.getElementById("GridView1");
var inputArray = gridViewObj.getElementsByTagName("input");
var inputArrayLength = inputArray.length;
for (a = 0; a < inputArrayLength) {
   if (inputArrayLength[a].type == "checkbox" && inputArrayLength[a].checked) {
      return true; 
   }
}

alert("You must check at least one checkbox sucka");
return false;


[monkey][snake] <.
 
kaht, Sorry, I didn't have a javascript function written and I thought if I listed the ID of my gridview that might be enough.

monksnake, Just one question.
I don't understand this line:
var inputArray = gridViewObj.getElementsByTagName("input");

How do I figure out what tag name is being used within my gridview?

Here is my code for my checkbox
Code:
<asp:TemplateField>
  <ItemTemplate>
    <asp:CheckBox ID="chkSelect" runat="server"/>
  </ItemTemplate>
</asp:TemplateField>
 
Since you have an ID for the checkbox and you only have one checkbox, the code will be much easier. You can directly check to see if the checkbox is checked via the ID.

Code:
var checkboxObj = document.getElementById("chkSelect");
if (!checkboxObj.checked) {
   alert("You must check at least one checkbox sucka");
   return false;
}
else {
  return true;
}

[monkey][snake] <.
 
Hey monksnake!

I really like your code snippet and would like to use it, but I'm not sure if it'll work for me.

I have a Repeater that has two table rows in it. The first row is the Master and when clicking on my "expand row" I populate and display the Gridview that is in the second row. I have a Checkbox in the Master row and also one in each of the rows of the Gridview. I would like to be able to check or uncheck all the items when the Master Checkbox is clicked.

I believe your code will work with one (unique) Gridview, however I could have many of them on the page at once. Is there a way to do this? Please let me know if you have any questions!

Thanks,

Jeff Tolman
E&M Electric
jeff.tolman@enm.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top