SIGN UP MEMBER LOGIN:    
ARTICLE

Developing a Multi-Select ASP.NET GridView using JQuery

Posted by Biswa Pujarini Mohapatra Articles | JQuery August 23, 2011
Step by step description to developing a Multi-Select ASP .net GridView using JQuery.
Reader Level:
Download Files:
 

Abstract

GridView is a very common control used in all .net Web Application. Also selecting a single row or multiple rows is a very common task in GridView operation. Most of the cases we do the Server Side Selection which is very pain-full. It's still more pain-full if we have a functionality for multi-select.

gridview.png

(Fig-1: Multi Select Gridview using Checkbox)

In some cases we add a Checkbox Control to get the selected rows at server side (see the above sample). But we always try for a Client Side row selection and Server Side row Manipulation.

Mult-Select using JQuery

Here, I used the power of JQuery Selector functionality to do the Client Side row Selection and a server side row Manipulation. Hope this will help you a lot in your GridView operation.

Step by step to implement Multiselect by using JQuery.

Step 1:

Download and add JQuery source file to your project. The download is available at http://jquery.com/

Add the JQuery reference to the Webpage.

<script src="jquery-1.4.3.js" type="text/javascript"></script>

Step 2:

Add an HiddenField to any of the TemplateField inside the GridView <Column> tag, which will store the selection mode (Here HiddenField will do the same operation like the CheckBox checked status).

Here I have added HiddenField along with the Id Column.

 <asp:TemplateField HeaderText="Id#" HeaderStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
<asp:HiddenField ID="hdnIsItemSelected" runat="server" />
</ItemTemplate>
</asp:TemplateField>

Step: 3

Within JavaScript block add the following code. This the main part where selection of the row is being handled.

<script type="text/javascript">
$(document).ready(function () {
$('input[id*=hdnIsItemSelected][value=true]')
.parent().parent().addClass('RowHighlight');
});

$(function () {
$('#MultiSelectGrid tr').mouseover(function () {
$('#MultiSelectGrid tr').removeClass('RowHover');
$(this).addClass('RowHover');
});
});

$(function () {
$('#MultiSelectGrid tr td').click(function () {
if ($(this).parent()[0].className == 'RowHover') {
$(this).parent().addClass('RowHighlight');
$('input[id*=hdnIsItemSelected]')[$(this).parent()[0]
.rowIndex - 1].value = 'true';
}
else {
$(this).parent().removeClass('RowHighlight');
$('input[id*=hdnIsItemSelected]')[$(this).parent()[0]
.rowIndex - 1].value = 'false';
}
});
});
</script>

Step: 4

Add the following CSS to your StyleSheet or inside the <style></style> of <head> tag. These CSS classes will control the mouse hover and selection effect of the Gridview.

 .RowHighlight
{
color: #336699;
cursor: pointer;
background-image: url('images/selected_item_back.png');
background-repeat: repeat-x;
}

.RowHover
{
color: #336699;
cursor: pointer;
background-image: url('images/hover_item_back.png');
background-repeat: repeat-x;
}


Step: 5

Now in the button click event or in the postback event, search each HiddenField status, if it's "true" it's selected else not selected.

foreach (GridViewRow row in MultiSelectGrid.Rows)
{
HiddenField selectionButton = (HiddenField)row.FindControl("hdnIsItemSelected");
if (selectionButton.Value == "true")
{
       
// do your operation here...

selectedName += (selectedName == "" ? "" : " / ") + ((Label)row.FindControl
("lblFirstName")).Text;

    }
}

Step: 6

Now run the application and see the impact of JQuery while selecting the row. In the given sample application I have added the DataSource for GridView in the Page_Load() Event.

Below two sample screens for Multi-Row Select and Single-Row Select of a Gridview.

Multi Select Gridview using JQuery

(Fig-2: Multi Select Gridview using JQuery)

Single row selection in Gridview using JQuery

(Fig-3: Single row selection in Gridview using JQuery)

Download the sample code and open it in VS-2010 and see how easy to understand the implementation.

Login to add your contents and source code to this article
share this article :
post comment
 

hi Biswa, Nice article

Posted by Rajamanikkam S Mar 31, 2012

It's amazing!!! Thanks!!!

Posted by Yuriy Jan 26, 2012

Thanks for your comment Krishna.

Posted by Biswa Pujarini Mohapatra Aug 25, 2011

Hi Biswa, This is really a nice article on GridView and JQuery. Can you please share some other articles on ASP .net control extension.

Posted by Balakrushna Swain Aug 25, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor