Showing posts with label ASP.NET Grid View. Show all posts
Showing posts with label ASP.NET Grid View. Show all posts

Tuesday, March 25, 2014

jQuery: Convert ASP.NET GridView Data into CSV

In this post, find jQuery code to convert ASP.NET GridView data into CSV format which is sometimes quite useful for exporting purpose. This can be also be done using server side code but that involves extra overhead "postback".

ASP.NET GridView control is rendered in table > th > tr > td format. The columns names are placed in th tag and all the data goes into various td tags. Also refer ebook on ASP.NET GridView & jQuery Tips and Tricks.


You may also like:

To create CSV, we need to loop through GridView including header and rows. While iterating, store the value in a array and in the end join the array with "," sign to form CSV.
$(document).ready(function() {
   $("#<%=txtCSV.ClientID%>").hide();
   $('#<%=btnSubmit.ClientID%>').click(function(e)
   {
     var arrCSV = [];
     var strTemp = '';
     $("#<%=gdRows.ClientID%>").find("tr").each(function () 
     {
       if ($(this).find("th").length) 
       {
         var arrHeader = [];
         $(this).find("th").each(function () {
           strTemp = $(this).text().replace(/"/g, '""');
           arrHeader.push('"' + strTemp + '"');
         });
       arrCSV.push(arrHeader.join(','));
      }
      else 
      {
        var arrData = [];
        $(this).find("td").each(function () {
         strTemp = $(this).text().replace(/"/g, '""');
         arrData.push('"' + strTemp + '"');
       });
       arrCSV.push(arrData.join(','));
      }
   });

    var strCSV = arrCSV.join('\n');
    $("#<%=txtCSV.ClientID%>").val(strCSV);
    $("#<%=txtCSV.ClientID%>").show();
    e.preventDefault();
   });
});
If you don't want to include headers in your CSV, then you can remove below code block from above code.
if ($(this).find("th").length) 
{
   var arrHeader = [];
   $(this).find("th").each(function () {
     strTemp = $(this).text().replace(/"/g, '""');
     arrHeader.push('"' + strTemp + '"');
   });
   arrCSV.push(arrHeader.join(','));
}
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Monday, July 29, 2013

ASP.NET GridView + jQuery Tips and Tricks

Read more...

Monday, April 1, 2013

jQuery: Get row and column index of a GridView Cell

In this post, Find jQuery code to get Row Index and Column Index of ASP.NET GridView Cell on click or on mouseover event.

Related Post:

To get the row and column index, attach mouseenter event to all tr and td of ASP.NET GridView. And using jQuery closest() find count of all the previous respective element.
$(document).ready(function () {
  $("#gdRows tr td").mouseenter(function () {
     var iColIndex= $(this).closest("tr td").prevAll("tr td").length;
     var iRowIndex = $(this).closest("tr").prevAll("tr").length;
   });
});
Keep in mind that both index starts from 0.
See Complete Code
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, February 20, 2013

Find control inside Asp.net GridView using jQuery

In this post, find jQuery code and explanation to "find control inside ASP.NET GridView". Why it is tricky? The reason is ID of control's placed inside ASP.NET Gridview get changed at the time of rendering. Till ASP.NET 3.5, the rendered client-side id is formed by taking the Web control's ID property and prefixed it with the ID properties of its naming containers.

In short, a Web control with an ID of txtFirstName can get rendered into an HTML element with a client-side id like "ctl00_MainContent_txtFirstName". And same is true for ASP.NET GridView.

Suppose, there is textbox and label control in gridview.
<ItemTemplate>
   <asp:TextBox ID="txtID" runat="server" />
   <asp:Label ID="lblID" runat="server" />
</ItemTemplate>
And when you run this, this is rendered as below.
<input name="gdRows$ctl02$txtID" type="text" id="gdRows_ctl02_txtID" />
<span id="gdRows_ctl02_lblID"></span>
As you notice, the ID of the control is changed. It is no more "txtID" or "lblID". Gridview ID and control number is added as prefix. So now how do you select these controls?

Related Post: To select all label which has ID as "lblID".
$('#<%=gdRows.ClientID %>').find('span[id$="lblID"]').text('Your text.');
To select particular label control out of all the labels which ends with "lblID".
var $arrL = $('#<%=gdRows.ClientID %>').find('span[id$="lblID"]');
var $lbl = $arrL[0];
$($lbl).text('Your text...');
Similarly, you can also find textbox. To select all textbox which has ID as "txtID".
$('#<%=gdRows.ClientID %>').find('input:text[id$="txtID"]').val('Your value.');
To select particular textbox control out of all the textboxes which ends with "lblID".
var $arrT = $('#<%=gdRows.ClientID %>').find('input:text[id$="txtID"]');
var $txt = $arrT[0];
$($txt).val('text...');
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Monday, February 11, 2013

Highlight Negative value columns in ASP.NET GridView using jQuery

In this post, find jQuery code to highlight negative value columns/cells in ASP.NET GridView. This is a helpful feature as it draws user's attention immediately and informs him that there is something wrong with some of the entities.


Related Post:

How to do it?


Define a style sheet class which will be used to highlight negative column value.
.negative
{
   font-weight: bold;
   color:red;
}
  • Iterate through all tr elements and select column which may have negative values. For demo, Quantity column is selected.
  • Cache the required column and check it's text. If text is numeric and less than 0 then apply style sheet to it.
So putting it together the complete jQuery code is,
$(document).ready(function () {
  $("#<%=gdRows.ClientID%> tr:has(td)").each(function () {
    var $tdElement = $(this).find("td:eq(2)"); //Cache Quantity column.
    var cellText = $tdElement.text();
    if ($.isNumeric(cellText)) 
    {
       var iNum = parseInt(cellText);
       if (iNum < 0) 
          $tdElement.addClass('negative');
    }
  });
});


Now, the above jQuery code is specific to a column and what if there are multiple columns which may contain negative values and you want to highlight them as well then above code will not work. The generic solution will be to iterate through all columns and see if they are negative. If yes, then highlight them.
$(document).ready(function () {
  $("#<%=gdRows.ClientID%> td").each(function () {
      var cellText = $(this).text();
      if ($.isNumeric(cellText)) 
      {
         var iNum = parseInt(cellText);
         if (iNum < 0) 
            $tdElement.addClass('negative');
      }
  });
});

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, January 16, 2013

Download ASP.NET GridView & jQuery Tips and Tricks eBook PDF

Being an ASP.NET developer and fan of jQuery, I had put together all my real life experience with ASP.NET GridView Control and jQuery and complied them in my first ever eBook named "ASP.NET GridView and jQuery Tips and Tricks". This eBook takes you through 16 different, useful, tips and tricks and offers solution for all possible requirements with ASP.NET GridView control. This is a point to point eBook with detailed explanation of code. Along with this eBook, code samples are also attached.

This eBook was not possible without my blog readers and followers. Your continuous support is encouraging and motivates to do more good things. "Thank You".


What you will learn from this book :
  1. Formatting ASP.NET GridView rows
  2. Highlight row on mouseover in ASP.NET GridView
  3. Set Alternate color for ASP.NET GridView columns
  4. Highlight ASP.NET GridView Rows when Checkbox is checked
  5. Change cursor to hand on mouseover in ASP.NET GridView
  6. How to remove ASP.NET GridView rows
  7. How to remove ASP.NET GridView columns
  8. Drag and Drop ASP.NET GridView rows
  9. How to loop through all ASP.NET GridView rows
  10. How to access particular cell in ASP.NET Gridview
  11. How to filter ASP.NET GridView records
  12. How to search through GridView records
  13. Get ASP.NET GridView Cell Value on Click
  14. Check/uncheck checkboxes in Asp.NET GridView
  15. Highlight empty table element
  16. Hide table rows with empty td element
Download eBook
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Thursday, January 3, 2013

Check/uncheck checkboxes in Asp.net GridView using jQuery

Find out jQuery code to check/uncheck or select/deselect all the checkboxes in ASP.NET Gridview. There are already plenty of articles on this topic but then how this is different. Assume, all the checkboxes are checked when header checkbox is checked but then you uncheck one of the child checkbox then what happens to your header checkbox? It should also get unchecked. Right? That's is where this post is different from others.

In this post, find jQuery code to
- Check/uncheck all child checkbox based on parent checkbox status.
- Update parent checkbox status based on child checkbox status.


Also Read:

How to do it?


  • Bind the click event to parent checkbox of ASP.NET GridView.
$('#gdRows').find('input:checkbox[id$="chkParent"]').click( function() {
});
  • In the click event, based on the parent checkbox status set the child checkbox status.
var isChecked = $(this).prop("checked");
$("#gdRows [id*=chkSelect]:checkbox").prop('checked',isChecked);
  • So the complete code for parent checkbox click event is,
$('#gdRows').find('input:checkbox[id$="chkParent"]').click( function() 
{
    var isChecked = $(this).prop("checked");
    $("#gdRows [id*=chkSelect]:checkbox").prop('checked',isChecked);
});
  • Now, based on the child checkbox status also need to update the parent checkbox. For example, if out of all the child checkboxes, one is unchecked then parent checkbox should also be checked.

    So for this, attach an click event handler to all the child checkboxes of ASP.NET GridView.
$('#gdRows').find('input:checkbox[id$="chkSelect"]').click(function() {
});
  • In this event, first define a flag with true value. And then loop through all the child checkbox and if one of the child checkbox is unchecked then set the flag value to false. And then update parent checkbox status value based on the flag variable value.
var flag = true;
$("#gdRows [id*=chkSelect]:checkbox").each(function() {
      if ($(this).prop("checked") == false) 
        flag = false;
});
$("#gdRows [id*=chkParent]:checkbox").prop('checked', flag);
So putting it together the complete jQuery code is,
$(document).ready(function() {
  
  $('#gdRows').find('input:checkbox[id$="chkParent"]').click(function() 
  {
     var isChecked = $(this).prop("checked");
     $("#gdRows [id*=chkSelect]:checkbox").prop('checked', isChecked);
  });
  
  
  $('#gdRows').find('input:checkbox[id$="chkSelect"]').click(function() 
  {
     var flag = true;
     $("#gdRows [id*=chkSelect]:checkbox").each(function() {
         if ($(this).prop("checked") == false) 
           flag = false;
     });
     $("#gdRows [id*=chkParent]:checkbox").prop('checked', flag);
  });

});​
See result below


Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Friday, December 21, 2012

jQuery to highlight GridView Rows when Checkbox is checked in ASP.NET

In this post, Find out jQuery code to highlight ASP.NET Gridview row when checkbox is checked and restore it to original state when unchecked.


Also Read:

How to do it?


  • Bind the click event to all the checkbox of ASP.NET GridView.
    $('#<%=gdRows.ClientID%>')
         .find('input:checkbox[id$="chkDelete"]')
         .click( function() {
    });
    
  • In the click event, first check whether the checkbox is checked or unchecked. And store the status in a variable.
    var isChecked = $(this).prop("checked");
  • After that find the respective row where checkbox is present. As we need to highlight that particular row only.
    var $selectedRow = $(this).parent("td").parent("tr");
  • If you have different color for alternate rows (see above image) then next few lines of code is required, otherwise you can skip it. For example, if all the rows of same color then skip this code. But if there are alternate color of GridView rows then it is required.

    As once we highlight the row, we can't find what was the previous color once it is unchecked. So the idea is to find the row index. Based on index value (even or odd) set color value in variable.
    var selectedIndex = $selectedRow[0].rowIndex;
    var sColor = '';
    if(selectedIndex%2 == 0)
        sColor = 'PaleGoldenrod';
    else
        sColor = 'LightGoldenrodYellow';
    
  • Now based on checkbox status, highlight the row (if checked). Otherwise restore it to previous color which is stored in sColor variable.
    if(isChecked)
        $selectedRow.css({
            "background-color" : "DarkSlateBlue",
            "color" : "GhostWhite"
        });
    else
       $selectedRow.css({
            "background-color" : sColor,
            "color" : "black"
        });
    
So putting it together the complete jQuery code is,
$(document).ready(function() 
{
  $('#<%=gdRows.ClientID%>')
     .find('input:checkbox[id$="chkDelete"]').click(function()
     {
        var isChecked = $(this).prop("checked");
        var $selectedRow = $(this).parent("td").parent("tr");
        var selectedIndex = $selectedRow[0].rowIndex;
        var sColor = '';
            
        if(selectedIndex%2 == 0)
            sColor = 'PaleGoldenrod';
        else
            sColor = 'LightGoldenrodYellow';
                
        if(isChecked)
            $selectedRow.css({
                "background-color" : "DarkSlateBlue",
                "color" : "GhostWhite"
            });
        else
           $selectedRow.css({
               "background-color" : sColor,
               "color" : "black"
          });
    });     
});
See result below


Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Friday, November 9, 2012

jQuery code to hide table rows with empty td element

Find jQuery code to hide those table rows (<tr>) which have at least one or more <td> element as empty or with no value. To hide table rows, iterate through all the td element and check it's text. If it is empty then hide it's parent (which is tr) using .hide().

Related Post:
$(document).ready(function() {
    $("#gdRows tr td").each(function() {
        var cellText = $.trim($(this).text());
        if (cellText.length == 0) {
            $(this).parent().hide();
        }
    });
});​
See result below


See Complete Code
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

jQuery code to highlight empty table element

Find jQuery code to highlight those <td> elements within a table/ Grid/ GridView/ DataGrid which have no value associated with it or which are empty.

All is required is to loop through all the <td> element and check it's value. If it is empty, then assign background color to it so that it looks highlighted.
$(document).ready(function() {
    $("#gdRows td").each(function() {
        var cellText = $(this).text();
        if ($.trim(cellText) == '') 
        {
            $(this).css('background-color', 'LightGreen');
        }
    });
});​
See result below


If you want then you can also assign some default data to these empty <td> elements. Using text() method set the default text.
$(document).ready(function() {
    $("#gdRows td").each(function() {
        var cellText = $(this).text();
        if ($.trim(cellText) == '') 
        {
            $(this).text('N/A');
        }
    });
});​
See Complete Code
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Wednesday, May 23, 2012

GridView and jQuery in ASP.NET

Read more...

Tuesday, May 8, 2012

Get ASP.NET GridView Cell Value on Click using jQuery

I had already posted more than 10 ASP.NET GridView Tips and Tricks with jQuery and today you will see How to Get ASP.NET GridView Cell Value when it is clicked, using jQuery. You might be knowing that ASP.NET GridView is rendered as table -> th -> tr -> td format. All the rows are converted into tr element and values placed in different td elements.


So, when the GridView Cell is clicked, for better user experience it is good to highlight the cell. You can also see the same in image. So Below given CSS class, I have used to highlight the selected cell.
//Code Starts
.selected
{
  background-color: Yellow;
  color : Green;
}
//Code Ends
Before attaching click event to the GridView cell's, it is important to let user know that cell is clickable. So change the mouse cursor style to "Pointer" so user will come to know that cell is clickable. Below code exactly does the same thing.
//Code Starts
$("#<%=gdRows.ClientID%> tr:has(td)").hover(function(e) {
   $(this).css("cursor", "pointer");
});  
//Code Ends
To get the Cell Data, it is important to find out that will cell is clicked. So using "closest()" method, we can find out. The "closest()" method gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
//Code Starts
$("#<%=gdRows.ClientID%> tr:has(td)").click(function(e) {
   var selTD = $(e.target).closest("td");
   $("#<%=gdRows.ClientID%> td").removeClass("selected");
   selTD.addClass("selected");
   $("#<%=lblSelected.ClientID%>").html(
       'Selected Cell Value is: <b> ' + selTD.text() + '</b>');
});
//Code Ends
The above jQuery code does the following things.
  • First assign a click event to all the tds of GridView.
  • After that it gets the clicked cell into a object using "closest()" method.
  • After it removes selected class from all the tds. This is important as if any cell was previously selected then remove the selected class.
  • Assign selected class to currently clicked cell.
  • And in the end, gets the selected cell's text and assign it to label.
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Monday, April 23, 2012

How to search through GridView records using jQuery

I had already posted "How to filter GridView records using jQuery" but With continuing my experiments with ASP.NET Grid View and jQuery, In this post I will show you how to search through all columns of ASP.NET GridView Data and show only those data which satisfies the search text. I have implemented it using jQuery so sharing with you.

Related Post:

Problem:

First take a look at below image.


As you can see from image, that there is a GridView control populated with data and above that there is a textbox and a button. So the problem was, based on the search text entered in search text box, filter the data from GridView on any of the column . For example, "10" is entered then only those rows should be visible which have 10 in any of the column. It can be in ID, ProductName, Price or quantity column.


Another example, if "L" is entered then below should be the output.


Note: Following controls are placed on the page.
  • A GridView
  • A Label control which has "No records to display" message.
  • And separate link button for each alphabet and one link button for "All".

Solution:

Below are detailed steps to how to do it.

  • First, hide the "No records to display" label and also hide all the rows of GridView.
// Hide No records to display label.
$('#<%=lblNoRecords.ClientID%>').css('display','none'); 
//Hide all the rows.
$("#<%=gdRows.ClientID%> tr:has(td)").hide(); 
  • Now declare a counter variable, which will be used to display "No records to display" label. And also get the search textbox value.
var iCounter = 0;
//Get the search box value
var sSearchTerm = $('#<%=txtSearch.ClientID%>').val();
  • If nothing is entered in search textbox, then display all gridview rows and return from here. You can also do validation to show alert message to user to enter something. But I have used Search button as Reset button as well so when nothing is entered, display all the GridView rows.
//if nothing is entered then show all the rows.
if(sSearchTerm.length == 0) 
{
  $("#<%=gdRows.ClientID%> tr:has(td)").show(); 
  return false;
}
//
  • Now run a loop through all the td element of GridView.
//Iterate through all the td.
$("#<%=gdRows.ClientID%> tr:has(td)").children().each(function() 
{
});
//
  • Now within the loop get the td value and compare if the td value matches with the search term. If Yes, then show it's parent (that is tr) and increment the counter variable.
//Iterate through all the td.
$("#<%=gdRows.ClientID%> tr:has(td)").children().each(function() 
{
    var cellText = $(this).text().toLowerCase();
    //Check if data matches
    if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) 
    {    
        $(this).parent().show();
        iCounter++;
        return true;
    } 
});
//
  • Finally, check if counter variable value is equal to 0. If Yes, then display "No records to display" label.
if(iCounter == 0)
{
   $('#<%=lblNoRecords.ClientID%>').css('display','');
}
//
So putting it together the complete jQuery code is,
//Code Starts
$(document).ready(function() {
    $('#<%=lblNoRecords.ClientID%>').css('display','none');

    $('#<%=btnSubmit.ClientID%>').click(function(e)
    {
        // Hide No records to display label.
        $('#<%=lblNoRecords.ClientID%>').css('display','none'); 
        //Hide all the rows.
        $("#<%=gdRows.ClientID%> tr:has(td)").hide(); 
        
        var iCounter = 0;
        //Get the search box value
        var sSearchTerm = $('#<%=txtSearch.ClientID%>').val(); 
        
        //if nothing is entered then show all the rows.
        if(sSearchTerm.length == 0) 
        {
          $("#<%=gdRows.ClientID%> tr:has(td)").show(); 
          return false;
        }
        //Iterate through all the td.
        $("#<%=gdRows.ClientID%> tr:has(td)").children().each(function() 
        {
            var cellText = $(this).text().toLowerCase();
            //Check if data matches
            if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) 
            {    
                $(this).parent().show();
                iCounter++;
                return true;
            } 
        });
        if(iCounter == 0)
        {
            $('#<%=lblNoRecords.ClientID%>').css('display','');
        }
        e.preventDefault();
    })
})
//Code Ends
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Tuesday, April 17, 2012

Set Alternate color for GridView columns using jQuery

In this post, we will see how easily we can assign background color to ASP.NET Grid Views columns using jQuery. In this example, we will assign "Tan" color to all the even columns of GridViews and "PaleGoldenrod" to even columns. When I say Odd, that means columns which are having odd numbers like column1, column3 etc.

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> td:nth-child(odd)").css("background-color", "Tan");
  $("#<%=gdRows.ClientID%> td:nth-child(even)").css("background-color", "PaleGoldenrod");
});
Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Tuesday, April 10, 2012

Updated ASP.NET GridView Tips and Tricks with jQuery

Read more...

Monday, April 9, 2012

How to access particular cell in gridview using jQuery

You might be knowing that GridView is rendered as table > th > tr > td format. Below jQuery code allows to select first cell or td of every row (tr) in GridView. I have used "eq()" selector to select particular cell.

Also read GridView Tips and Tricks using jQuery
//Code Starts
$(document).ready(function() {
   $("#<%=gdRows.ClientID%> tr:has(td)").each(function() {
      var cell = $(this).find("td:eq(0)");
      alert(cell.html());
   });
});
//Code Ends
With ":eq()" selector you can pass the index of element to select. The ":eq()" selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). So if you want to select 2nd column then use :eq(1) as the selector.

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

How to filter GridView records using jQuery

Yesterday, I got into a situation where I need to filter the rows/records of the ASP.NET GridView on client side. When I say filter, that means depending on some condition just show only those records which satisfies the condition. And I implemented the same using jQuery so thought of sharing with you.

Problem:

First take a look at below image.


As you can see from image, that there is a GridView control populated with data and above that there are link buttons for each alphabet along with "All" button. So the problem was, based on the clicked button filter the data from GridView on "ProductName" column. For example, if "H" is clicked, only those products should be visible whose name starts with "H".


And if there are no records that satisfies the condition, then display no records message.


Note: Following controls are placed on the page.
  • A GridView
  • A Label control which has "No records to display" message.
  • And separate link button for each alphabet and one link button for "All".

Solution:

To implement this feature the solution is,
  • When DOM is ready then hide the Label control.
  • Attach click event for Link buttons. I have assigned a class "links" to all link button. So using class selector, we can attach the click event.
  • Get the text of the clicked button.
  • Now loop through all the gridviews rows and find td with Product Name.
  • Check if the text of td starts with cliked button's text.
  • If yes, then don't hide the row. Otherwise, hide the row.

But we also need to handle the "All" button code and when there are no records then show the label control. The code is very much self explanatory.
//Code Starts
$(document).ready(function() {
    $('#<%=lblNoRecords.ClientID%>').css('display','none');

    $('.links').click(function(e)
    {
        $('#<%=lblNoRecords.ClientID%>').css('display','none'); 

        var lnkText = $(this).text().toLowerCase();
        var iCounter = 0;

        $("#<%=gdRows.ClientID%> tr:has(td)").each(function() {
            var cell = $(this).find("td:eq(1)").text().toLowerCase();
            if(lnkText != 'all')
            {
                if(cell.indexOf(lnkText) != 0)
                {
                    $(this).css('display','none');
                }
                else
                {
                    $(this).css('display','');
                    iCounter++;
                }    
            }
            else
            {
                $(this).css('display','');
                iCounter++;
            }
        });

        if(iCounter == 0)
        {
            $('#<%=lblNoRecords.ClientID%>').css('display','');
        }
        e.preventDefault();
    });
});
//Code Ends
Also read GridView Tips and Tricks using jQuery

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Tuesday, March 20, 2012

How to loop through all grid view rows using jQuery

In this post, I will show you that how can you loop through individual rows of ASP.NET GridView using jQuery. You might be knowing that GridView is rendered as table > th > tr > td format. The columns names are placed in th tag and all the data goes into various td tags. So when you want to loop through all the rows then, just find the rows which have td and are part of ID of your GridView.

Also read GridView Tips and Tricks using jQuery

See below jQuery Code. In this code, the ID of GridView is "gdRows".
$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr:has(td)").each(function() {
       alert($(this).html());
  });
});
If you want to loop through all the rows including th as well, then use below code.
$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr").each(function() {
       alert($(this).html());
  });
});
And lastly, if you want to access only th row then
$(document).ready(function() {
  $("#<%=gdRows.ClientID%> th").each(function() {
       alert($(this).html());
  });
});
Also read GridView Tips and Tricks using jQuery

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Tuesday, March 6, 2012

Some GridView Tips and Tricks using jQuery

GridView control is quite useful and most commonly used control in ASP.NET application. In this post, I have put together collection of GridView tips and tricks with jQuery. These tips and tricks are quite useful in your daily code and you can improve the user experience using this tips and tricks.


I hope these tips will help you in your work. If you also have any tips (other then above) then feel free to share your GridView tips and trick.

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...

Drag and Drop GridView rows using jQuery

With continuation with my Grid View series posts, In this post, I will show you how to rearrange the grid view rows via dragging and dropping rows using jQuery. For your information, GridView is rendered as table format. So to support drag and drop functionality to table rows, we will use plugin named "TableDnD". This plugin plugin allows the user to reorder rows within a table. You can download this plugin from here.

All you need to do is to call the function, "tableDnD()" on the grid view and you are done. See below jQuery code.
$(document).ready(function() {
   $("#<%=gdRows.ClientID%>").tableDnD();
});
You can also add style to let user know that which rows is selected and dropped. As in below image, the 4th row is selected.
To do this, you need to assign a css class to "onDragClass" option of this plugin. See below jQuery code.
$(document).ready(function() {
   $("#<%=gdRows.ClientID%>").tableDnD({
     onDragClass: "myDragClass"
   });
});
Hope you have find this useful!!!!!!
To find out all the available option with this plugin, read this article.

Feel free to contact me for any help related to jQuery, I will gladly help you.
Read more...