Using JavaScript To Select GridView Rows

This article is code for select rows in GridView by using JavaScript and C# language. It is every important by when you select on this row it is show all data in cell in Grid View to Textbox. add when select on each row it always show highlights on row that you select.

1. You must have one table that have data

ID

Name

Sex

001

Theary

F

002

Dara

M

003

Bunney

F

004

Kakda

M

005

Sokhut

M

2. Open Visual Basic and Create New Project and write code below:

a. In .aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SystemCodingScheme.aspx.cs" Inherits="PerfexBankSystem.SystemCodingScheme" EnableEventValidation="false" %>

Make sure you have this code “EnableEventValidation="false"” in this tage in your page.

you must have JavaScript Code for Hightlights on row in Grid View

3. In aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
//-------------------For call Function of JavaScript for show hightlights on row-----------------------
    protected void GridView1_RowDataBound(object sender, 
                    GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink (this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }
//-------------------/For call Function of JavaScript for show hightlights on row-----------------------
 
// --------------------Event when you select on each row in Grid View------------------
 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       //-----------For recieve data from cell in gridview in textbox------------
        TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
        TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
        TextBox3.Text = GridView1.SelectedRow.Cells[2.Text;
    }
// --------------------/Event when you select on each row in Grid View------------------
 
}

4. Show Result below

ID

Name

Sex

001

Theary

F

002

Dara

M

003

Bunney

F

004

Kakda

M

005

Sokhut

M

No comments:

Post a Comment