Click here to Skip to main content
15,885,180 members
Articles / Web Development / ASP.NET

Row level JavaScript for GridView

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
8 Oct 2006CPOL 79.2K   235   15   12
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
//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.

C#
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:

JavaScript
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:

C#
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)


Written By
Web Developer
United States United States
Nothing special about me.

Comments and Discussions

 
QuestionCan I do something similar to hide/show columns? Pin
Marlene Deyo18-Jun-09 10:34
Marlene Deyo18-Jun-09 10:34 
AnswerRe: Can I do something similar to hide/show columns? Pin
Arjun Arora18-Jun-09 14:50
Arjun Arora18-Jun-09 14:50 
GeneralPlease help.Its very urgent Pin
Rathan Kumar7-Jun-09 20:33
Rathan Kumar7-Jun-09 20:33 
GeneralEnable Disable Controls Pin
Love11114-Nov-08 1:50
Love11114-Nov-08 1:50 
Questionnot working in firefox Pin
modeerftoall14-May-08 19:32
modeerftoall14-May-08 19:32 
QuestionHow Do I Make This Work With Drop Down List? Pin
tcannon6-Nov-07 3:50
tcannon6-Nov-07 3:50 
GeneralJava script not working in FireFox Pin
T.Ashraf14-Aug-07 13:31
T.Ashraf14-Aug-07 13:31 
GeneralRe: Java script not working in FireFox Pin
ArjunSingh14-Aug-07 14:56
ArjunSingh14-Aug-07 14:56 
GeneralRe: Java script not working in FireFox [modified] Pin
T.Ashraf15-Aug-07 14:35
T.Ashraf15-Aug-07 14:35 
GeneralInvisible Fields Pin
dspyank29-Mar-07 11:00
dspyank29-Mar-07 11:00 
GeneralRe: Invisible Fields Pin
T.Ashraf14-Aug-07 9:04
T.Ashraf14-Aug-07 9:04 
QuestionCan script account for alternate row background color Pin
Timothy Vandeweerd20-Nov-06 11:41
Timothy Vandeweerd20-Nov-06 11:41 

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

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