Ashraf's Blog

Light and Power of Knowledge

Sponsors

Tags

News

HtmlAgility - an excellent HTML parser for DOM and XPATH or XSLT
Working with Argotic - a nice tool to manage Feed
Powerful .net zip library dotnetzip

JavaScript for GridView Row Level

Introduction:

I was working in a project where I need to calculate price according to the number of food selected from a drop down list and this will be client site operation. In that article in am writing code on how to access every row and cell from JavaScript, and also write some small operation using these row and cell.

1 2 3 4

Background:

Writing JavaScript is important in grid view where many operation is coming on dropdownlist clicking so needed to use JavaScript to make is faster and user friendly.

 

Code Step by Step:

Create a grid view first which will load data from database. here is the code of GridView

   1: <asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="False" ShowFooter="true">
   2:     <Columns>
   3:         <asp:TemplateField>
   4:             <ItemTemplate>
   5:                 <asp:CheckBox runat="server" ID="chkIT" onClick="changeColor(this)"/>
   6:             </ItemTemplate>
   7:         </asp:TemplateField>
   8:         <asp:TemplateField>
   9:             <ItemTemplate>
  10:                 <asp:DropDownList ID="DropDownList1" runat="server" onchange="showData(this)">
  11:                     <asp:ListItem Selected="True" Text="1" Value="1"></asp:ListItem>
  12:                     <asp:ListItem Text="2" Value="2"></asp:ListItem>
  13:                     <asp:ListItem Text="3" Value="3"></asp:ListItem>
  14:                     <asp:ListItem Text="4" Value="4"></asp:ListItem>
  15:                     <asp:ListItem Text="5" Value="5"></asp:ListItem>
  16:                 </asp:DropDownList>
  17:             </ItemTemplate>
  18:         </asp:TemplateField>
  19:         <asp:TemplateField HeaderText="Id"> 
  20:             <ItemTemplate>
  21:                 <asp:Label ID="lblData" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
  22:             </ItemTemplate>
  23:         </asp:TemplateField>
  24:         <asp:TemplateField HeaderText="Name">
  25:             <ItemTemplate>
  26:                 <asp:Label ID="lblData" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
  27:             </ItemTemplate>
  28:         </asp:TemplateField>
  29:         <asp:TemplateField HeaderText="Price/Unit">
  30:             <ItemTemplate>
  31:                 <asp:Label ID="lblData" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
  32:             </ItemTemplate>
  33:             <FooterTemplate>
  34:                 <asp:Label ID="lblTotalPrice" runat="server" Text="Total"></asp:Label>
  35:             </FooterTemplate>
  36:         </asp:TemplateField>
  37:         <asp:TemplateField HeaderText="Price">
  38:             <ItemTemplate>
  39:                 <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
  40:             </ItemTemplate>
  41:             <FooterTemplate>
  42:                 <asp:Label ID="lblTotalPrice" runat="server" Text=""></asp:Label>
  43:             </FooterTemplate>
  44:         </asp:TemplateField>
  45:     </Columns>
  46: </asp:GridView>

here from the grid view I have  called 2 JavaScript method to

1. showData(this) is called from onchange metod of drop down list which is in the Item Template of GridView. This method is showing change value using javascript

2. changeColor(this) is called from onClick method of Check box this is just to show how to set style form JavaScript in a row of GridView.

here is the JavaScript Code is working for GridView

   1: <script language="javascript" type="text/javascript">
   2: function showData(dropdown) 
   3: {
   4:     //get dropdownlist
   5:     var myindex  = dropdown.selectedIndex;
   6:     var SelValue = dropdown.options[myindex].value;
   7:     //get the row of the drop down list
   8:     var row = getParentRow(dropdown);
   9:     var newRate;
  10:     //set value in the cell of grid view
  11:     if (isFireFox()) 
  12:     {  
  13:         newRate = row.cells[4].textContent * SelValue;
  14:     }
  15:     else 
  16:     {
  17:         newRate = row.cells[4].innerText * SelValue;
  18:     }
  19:     row.cells[5].innerHTML = newRate;
  20:     //get gridview
  21:     var gridViewCtlId = '<%=myGrid. ClientID%>';
  22:     var grid = document.getElementById(gridViewCtlId);
  23:     var gridLength = grid.rows.length;
  24:     var sum = 0;
  25:     //calculate sum of the prices looping through the gridview and operation in every cell
  26:     for (var i = 1; i < gridLength-1; i++) 
  27:     {
  28:         if (isFireFox()) {
  29:             sum = sum + parseInt(grid.rows[i].cells[5].textContent);
  30:         }else {
  31:             sum = sum + parseInt(grid.rows[i].cells[5].innerText);
  32:         }
  33:     }
  34:     grid.rows[gridLength-1].cells[5].innerHTML = sum;
  35: }
  36: function getParentRow(obj)
  37: {
  38:     while(obj.tagName != "TR")
  39:     {
  40:         if(isFireFox())
  41:         {
  42:             obj = obj.parentNode;
  43:         }
  44:         else
  45:         {
  46:             obj = obj.parentElement;
  47:         }
  48:     }
  49:     return obj;
  50: }
  51: function isFireFox()
  52: {
  53:     return navigator.appName == "Netscape";
  54: }
  55: function changeColor(obj) 
  56: {
  57:     var rowObject = getParentRow(obj);
  58:     if (obj.checked) {
  59:         rowObject.style.backgroundColor = 'Yellow';
  60:     }else {
  61:         rowObject.style.backgroundColor = '';
  62:     }
  63: }
  64: </script>

Comments

JavaScript for GridView Row Level Access « Ashraf’s Blog said:

Pingback from  JavaScript for GridView Row Level Access &laquo; Ashraf&#8217;s Blog

# February 25, 2009 10:45 PM

kinjalin said:

Good piece of Code!! Ashraf

# March 18, 2009 7:33 AM

Dani said:

Neat and exactly what I need now. Can I ask more? :)

# June 11, 2009 12:54 PM

Ashrafur Rahaman said:

Hi Dany, Please ask me if you have any query :)

# June 11, 2009 1:55 PM

mohammed imran said:

hello bro i am mohammed imran working on asp.net and i am having the task to change the gridview control values into multilanguage so can u help me out,plz bro

# June 14, 2009 3:51 AM

Ahaaaaaaaaaaaaaa said:

lllllllllllllllllll

# August 17, 2009 7:28 AM

Sb Vivek said:

hi,

   your article is very helpful. Thanks. In all your article u used different keywords for Firefox and other browsers. where can i get these information like which browser supports what keywords. Please advise me. Im a "Newbie".

# December 30, 2009 3:39 AM

bcs said:

Very Good. Just what i was looking for.

Can you pl tell me why the ' while(obj.tagName != "TR") ' loop  for?

# December 31, 2009 3:14 AM

Srikanth said:

Super article brother

# March 23, 2010 5:09 AM

Chloe ADAMS said:

Really cool story you got here. It'd be just great to read a bit more about that topic. Thanx for sharing such info.

Chloe ADAMS

<a href=" www.renttobuyguide.co.uk/.../a>

# April 28, 2010 11:15 AM

Mia Thompson said:

Indeed nice post u have here. It'd be really cool to read a bit more concerning this topic. Thanks for posting such material.

Mia Thompson

<a href="www.waybiz.com/">B2B marketing</a>

# May 4, 2010 6:07 AM

Julia Swenson said:

It is certainly interesting for me to read this blog. Thank author for it. I like such topics and anything that is connected to them. I definitely want to read a bit more soon. BTW, pretty nice design this site has, but how about changing it once in a few months?

Julia Swenson

<a href="www.baccaratgirls.com/">escorts in central london</a>

# May 4, 2010 12:10 PM

block mobile phone signal said:

And how about adding some more pictures?  I’m not trying to offend anyone, content is really nice. Just as I’ve heard visitors acquire info much more effective if they see some useful images.

Lenny Tayfon

# May 12, 2010 2:50 PM

Mushari said:

Thanks a lot

it is help full subject

# May 21, 2010 4:00 AM

indian escort holborn said:

Truly interesting blog to read it to my thinking. The only question I have, why haven't you you add this article to social bookmarking sites? This may bring rather big traffic to this domain.

# May 30, 2010 7:54 AM

escorts female said:

Really great post to pay attention to as for me. BTW, why haven't you you add it to social media? That can bring rather big traffic to this blog.

# June 17, 2010 12:06 AM

brunet girls said:

Really great story u have here. I'd like to read more concerning such topic. Thanx for posting this info.

Amelia LONG

# June 24, 2010 1:00 AM

escort dating said:

Rather nice place you've got here. Thanks the author for it. I like such topics and everything connected to them. I definitely want to read a bit more on that blog soon.

Joan Smith

# September 2, 2010 12:13 AM

Julia Benedict said:

It is rather interesting for me to read the blog. Thanx for it. I like such themes and anything that is connected to this matter. I definitely want to read more on that blog soon.

Julia Benedict

<a href="nycescort.org/">escorts in syracuse ny</a>

# September 8, 2010 11:54 PM

Kiev escort girls said:

Cool blog you got here.

I'd like to read a bit more about this matter.

Thanx for posting that material.

With best regards Nelya!

# September 19, 2010 1:59 AM

Anete Benedict said:

It was rather interesting for me to read that post. Thanx for it. I like such themes and everything that is connected to this matter. I would like to read more soon.

Anete Benedict

<a href="escorttweets.com/">girl paradise</a>

# September 22, 2010 10:21 AM

Kate Hakkinen said:

It was certainly interesting for me to read that blog. Thanx for it. I like such topics and everything connected to them. I definitely want to read a bit more soon.

Kate Hakkinen

<a href="kievcityescort.com/">escorts ukraine</a>

# September 26, 2010 11:53 PM

Anete Simpson said:

It was certainly interesting for me to read that post. Thanks for it. I like such themes and everything connected to them. I would like to read more on that blog soon.

Anete Simpson

<a href="irelandescortdirectory.com/.../belfast-escorts">bruna escort belfast</a>

# October 14, 2010 3:16 AM

Kate Smith said:

Rather cool site you've got here. Thanks for it. I like such topics and anything connected to this matter. I would like to read a bit more on that blog soon.

What  do you think about changing it from time to time?

Kate Smith

<a href="www.asianescort.us/">asian escort services nyc</a>

# October 29, 2010 1:28 AM

Kate Benedict said:

Rather interesting blog you've got here. Thanx for it. I like such themes and anything that is connected to them. I would like to read more soon.

Don't you think design should be changed from time to time?

Kate Benedict

<a href="monacoescort.com/">escort monaco</a>

# November 29, 2010 1:38 PM

Avril Smith said:

Great content. Waiting for you to continue the topic.    

Avril Smith  

<a href="westsideescort.com/">oriental escorts west london</a>

# December 4, 2010 2:26 PM

Hilary Benedict said:

Rather nice place you've got here. Thanks for it. I like such topics and anything that is connected to this matter. I would like to read more soon.

Hilary Benedict

<a href="irelandescortdirectory.com/">www escort ireland com</a>

# December 4, 2010 4:27 PM

Jenny Swingfield said:

Wow, rather interesting article. How will I get this subscription?

Jenny  Swingfield

<a href="www.latinescortlondon.com/">London latin escort</a>

# December 9, 2010 7:08 AM

Joan Smith said:

Impressive post. Keep posting that way.  

Joan Smith    

<a href="mexicoescorts.biz/">escorts en mexico</a>

# January 9, 2011 5:45 PM

Kate Smith said:

Impressive page. Ready to read more.    

Kate Smith      

<a href="kievcityescort.com/">escort lady odessa ukraine</a>

# January 20, 2011 8:42 AM

Kate Swenson said:

Pretty nice place you've got here. Thanks for it. I like such topics and anything that is connected to them. I definitely want to read more on that blog soon.

Kate Swenson    

<a href="milanescorts.com/">accompagnatrici escort milano</a>

# February 23, 2011 4:24 PM

Home Security Monitoring software said:

I'll immediately grab your rss as I can not find your e-mail subscription link or e-newsletter service. Do you have any' Kindly let me know so that I could subscribe. Thanks.

<b><a href="www.crankinfo.com/.../why-consumers-love-home-security.html

">Home Security Monitoring

<a/><b/>

# March 30, 2011 1:47 PM

weblogs.asp.net said:

Javascript for gridview row level.. Great! :)

# June 1, 2011 3:28 PM

tryecrot said:

Yes there should realize the opportunity to RSS commentary, quite simply, CMS is another on the blog.

# August 28, 2011 3:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)