Click here to Skip to main content
Licence CPOL
First Posted 8 Oct 2006
Views 56,445
Downloads 91
Bookmarked 15 times

Row level JavaScript for GridView

By | 8 Oct 2006 | Article
This article expalins how to attach JavaScript functions to GridView rows and fields.

Introduction

This sample explains how to attach JavaScript methods/functions to an ASP.NET GridView control at row level. The user experience here is far better, and the client handles user actions, and there are fewer postbacks. In this sample, there is a checkbox on the grid. When the user checks the checkbox, the row color changes, indicating the user that the particular record is selected. When the user selects an option in the column 'Discontinued?', the text box in the column 'Reason to Discontinue?' for that row shows up.

Sample Image - GridViewJavascript.gif

//Javascript function to change background color of GridRow
function ColorRow(CheckBoxObj)
{   
    if (CheckBoxObj.checked == true) {
        CheckBoxObj.parentElement.parentElement.style.backgroundColor='#88AAFF';
    }
    else
    {
        CheckBoxObj.parentElement.parentElement.style.backgroundColor='#FFFFFF';
    }
}

This method is attached to checkbox on the rowDatabound event of the GridView.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    ((CheckBox)e.Row.FindControl("CheckMark")).Attributes.Add(
               "onClick", "ColorRow(this)");
}

The other functionality demonstrated here is how to make the control visible and invisible based on user selection. When the user selects the Yes option in the column Discontinued, the textbox for the explanation becomes visible. The JavaScript to toggle control visibility is as follows:

function ShowHideField(DecisionControl, ToggleControl)
{   
    var DecisionValue = getRadioSelectedValue(DecisionControl);
    if (DecisionValue =='True') 
    {
        ToggleControl.style.visibility='visible';
    }
    else
    {
        ToggleControl.style.visibility='hidden';
    }
}

This JavaScript method/function is attached in the RowDataBound event of the GridView as follows:

if (e.Row.RowType == DataControlRowType.DataRow)
{
   TextBox  txtReasonDiscontinue =  
      ((TextBox)e.Row.FindControl("ReasonDiscontinue"));
   RadioButtonList rblDiscontinue = 
      ((RadioButtonList)e.Row.FindControl("rblDiscontinued"));
   string strDicontinued = rblDiscontinue.SelectedValue;
   if (String.Compare(strDicontinued.Trim(), "False", true) == 0)
   {
        txtReasonDiscontinue.Attributes.Add("Style", "visibility:hidden");
   }
   rblDiscontinue.Attributes.Add("onClick", 
       "ShowHideField(this," + txtReasonDiscontinue.ClientID + ")");
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arjun Arora

Web Developer

United States United States

Member

Nothing special about me.

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
QuestionCan I do something similar to hide/show columns? PinmemberMarlene Deyo10:34 18 Jun '09  
AnswerRe: Can I do something similar to hide/show columns? PinmemberArjun Arora14:50 18 Jun '09  
GeneralPlease help.Its very urgent PinmemberRathan Kumar20:33 7 Jun '09  
GeneralEnable Disable Controls PinmemberLove11111:50 4 Nov '08  
Questionnot working in firefox Pinmembermodeerftoall19:32 14 May '08  
QuestionHow Do I Make This Work With Drop Down List? Pinmembertcannon3:50 6 Nov '07  
GeneralJava script not working in FireFox PinmemberT.Ashraf13:31 14 Aug '07  
GeneralRe: Java script not working in FireFox PinmemberArjunSingh14:56 14 Aug '07  
GeneralRe: Java script not working in FireFox [modified] PinmemberT.Ashraf14:35 15 Aug '07  
GeneralInvisible Fields Pinmemberdspyank11:00 29 Mar '07  
GeneralRe: Invisible Fields PinmemberT.Ashraf9:04 14 Aug '07  
QuestionCan script account for alternate row background color PinmemberTim Vandeweerd11:41 20 Nov '06  

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
Web01 | 2.5.120528.1 | Last Updated 8 Oct 2006
Article Copyright 2006 by Arjun Arora
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid