Click here to Skip to main content
Licence 
First Posted 17 Mar 2006
Views 46,045
Bookmarked 24 times

Client-side validation of radio button column in a DataGrid

By | 17 Mar 2006 | Article
How to do client-side validation of radio button column in a DataGrid.

Introduction

Very often, it's required to have a radio button column in a DataGrid. A grid listing of user details along with a radio button, would be a typical example. The requirement may be to select a user from the list of users. On changing the user in the grid, the previous selection should be cancelled.

Achieving this at the client side will reduce the communication between the server and the client and will thereby reduce the network traffic.

The following example will explain how to achieve this.

Section 1 : ASPX code

The user details are fetched from the database and filled to a DataSet. The DataSet is made the data source for the DataGrid.

<form id="frmSecurity" method="post" runat="server">

<asp:datagrid id="dgrUserAcctList" runat="server" GridLines="Horizontal" >
 <Columns>
  <asp:TemplateColumn>
    <ItemTemplate>
      <asp:RadioButton id="radUserAccount" runat="server" Text=''></asp:RadioButton>
    </ItemTemplate>
  </asp:TemplateColumn>
  <asp:BoundColumn Visible = "False" DataField="USER_ID"></asp:BoundColumn>
  <asp:BoundColumn DataField="USER_NAME" HeaderText="User” </asp:BoundColumn>
  <asp:BoundColumn DataField="USER_DEPT" HeaderText="Deparatment "></asp:BoundColumn>
  <asp:BoundColumn DataField="USER_ROLE" HeaderText="Role"></asp:BoundColumn>
  </Columns>
</asp:datagrid>
</form>

Section 2 : Code behind (ASPX.vb)

At the code-behind the ItemDataBound event of the DataGrid is implemented as below:

Private sub dgrUserAcctList _ItemDataBound(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
        Handles dgrUserAcctList.ItemDataBound
    Try
        If e.Item.ItemIndex > -1 Then
            Dim strSelRadio As String = _
                e.Item.Cells(0).FindControl("radUserAccount ").ClientID()
            CType(e.Item.FindControl("radUserAccount "), _
                RadioButton).Attributes.Add("onClick", _
                "fnCheckSel('" + strSelRadio + "')")
        End If
    Catch ex As Exception
        //Exception handling 
    End Try
End Sub

Section 3: JavaScript code

Now the client side validation using JavaScript can be written as:

function fnCheckSel(intObjId)
{
 var strSceTypeId; 
 strSceTypeId = intObjId + "1"
 for (var i=1; i<document.forms(0).length; i++)  
  { if(document.forms(0).elements[i].id )
   { if(document.forms(0).elements[i].id.indexOf("radUserAccount")!=-1)
    {if (document.forms(0).elements[i].id.indexOf("radUserAccount1")==-1)
     {document.forms(0).elements[i].checked=false
      }}}} //end for
 document.getElementById(intObjId).checked=true
}//end function

In the same manner, if the selection of atleast one user is mandatory, the validation at the form submission can be done using the function below:

function fnCheckVal(intObjId)
{
 var found_it ;
 for (var i=1; i<document.forms(0).length; i++)   
 { if(document.forms(0).elements[i].id )
  { if(document.forms(0).elements[i].id.indexOf("radUserAccount")!=-1) 
   {if (document.forms(0).elements[i].checked)
    {// Set the flag if any radio button is checked
       found_it = true;
       break;
     }}}} 
 if(!found_it)
 {alert("Select an Account");
  return false;
 }
 else 
 {return true;
  } 
}

That's all.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

gn_srini



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNeed to capture the data on the server event PinmemberSusindran_t11:38 15 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 17 Mar 2006
Article Copyright 2006 by gn_srini
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid