• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 602
  • Last Modified:

Validation in datagrid with checkbox

i have a datagrid and inside datagrid i have checkbox
to select and deselect the item that i want to delete.
I have a delete button.
When delete button is pressed,it should validate for
atleast one checkbox should be selected.

I don't know how to achieve this validation.

Could you please help me.

Here is my code.
-------------------------------------------------------------------
<form id="Form1" method="post" runat="server">
                                    <TABLE height="179" cellSpacing="0" cellPadding="0" width="456" border="0" ms_2d_layout="TRUE">
                                          <TR vAlign="top">
                                                <TD width="10" height="15"></TD>
                                                <TD width="446"></TD>
                                          </TR>
                                          <TR vAlign="top">
                                                <TD height="139"></TD>
                                                <TD>
                                                      <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
                                                            <Columns>
                                                                  <asp:TemplateColumn>
                                                                        <HeaderTemplate>
                                                                              <asp:CheckBox ID="checkall" Runat="server" onclick="select_deselect(this.checked,this.id)"></asp:CheckBox>
                                                                        </HeaderTemplate>
                                                                        <ItemTemplate>
                                                                              <asp:CheckBox ID="deletethis" Runat="server"></asp:CheckBox>
                                                                        </ItemTemplate>
                                                                  </asp:TemplateColumn>
                                                                  <asp:TemplateColumn>
                                                                        <HeaderTemplate>
                                                                              ID
                                                                        </HeaderTemplate>
                                                                        <ItemTemplate>
                                                                              <asp:Label ID="perskey" Text='<%# DataBinder.Eval(Container.DataItem,"Pers_Key")%>' Runat=server>
                                                                              </asp:Label>
                                                                        </ItemTemplate>
                                                                  </asp:TemplateColumn>
                                                                  <asp:BoundColumn HeaderText="LastName" DataField="legal_last_name"></asp:BoundColumn>
                                                                  <asp:BoundColumn HeaderText="FirstName" DataField="legal_first_name"></asp:BoundColumn>
                                                                  <asp:BoundColumn HeaderText="City" DataField="City"></asp:BoundColumn>
                                                                  <asp:BoundColumn HeaderText="State" DataField="State"></asp:BoundColumn>
                                                                  <asp:BoundColumn HeaderText="Zip" DataField="Zip_Cd"></asp:BoundColumn>
                                                            </Columns>
                                                      </asp:DataGrid></TD>
                                          </TR>
                                          <TR vAlign="top">
                                                <TD height="25"></TD>
                                                <TD>
                                                      <asp:Button ID="DeleteItem" Text="DeleteItem" Runat="server"></asp:Button>                                                      
                                                      </TD>
                                                      
                                          </TR>
                                    </TABLE>
                              </form>
----------------------------------------------------------------  
0
r_pat72
Asked:
r_pat72
1 Solution
 
Sam_JayanderCommented:
Hi,
you have two methods to solve this.

1. If you have no other checkboxed in you form, then you can use this format,

function CheckSel()
{
      var frm = document.Form1;
      for(i=0;i< frm.length;i++)
      {
            e=frm.elements[i];
            if ( e.type=='checkbox' && e.name.indexOf('Id') != -1 )
                  if(e.checked== false)
                  {
                        return true;
                  }
      }
      alert("Select atleast one check box.");
      return false;
}

In the above code snippet, change the name for the form(Form1) to the name of your form.  This will check either a checkbox is selected in the form.

===============================================================
2. If you have any other checkboxes, then you should check for appropriate controls.

for(rowCounter=2;rowCounter<=ClientsideGridname.rows.length;rowCounter++)
{
      chkBox = "clientsidegridname"+rowCounter + "clientsidecontrolname";
      if (document.getElementById(chkBox).checked == true)
      {
            return true;
      }
      alert("Select atleast one check box.");
      return false;
}

when the datagrid is rendered, the names are customised on client side, you can use viewsource to see the clienside name of the table (datagird). usually it will be a table with the name of the datagrid.

and, all the controls, including the checkbox will have name as like 'gridname__ctl[rowcount]_controlname'.

so while iterating the controls on clientside you should replace the "ClientsideGridname" and "clientsidecontrolname" appropriately. this will help you, only if the grid is shown on a page.  This will not work for usercontrols.

Hope this would help you.

Regards,
--Sam
0
 
r_pat72Author Commented:
I wanted to do this validation using server side validation controls like requiredfield validators .Is it possible validating using server side validation.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now