Solved

jQuery and GridView question

Posted on 2011-03-15
3
426 Views
Last Modified: 2012-05-11
Hi,

I am trying to use jQuery with a GridView but having trouble making it work.  The markup for a gridView looks as follows (abbreviated version):

<asp:TemplateField HeaderText="Start Date">
<EditItemTemplate>
   <asp:Button runat="server" ID="btnShowCalendar" CssClass="CalendarDiv" onClientClick="return false" Text="..." />

   <div style=display:none">
      <asp:Calendar id="calStartDate" OnSelectionChanged="calDate_SelectionChanged" Runat="server" />
   </div>
</EditItemTemplate>
</asp:TemplateField>

Similiar code has been applied to several other date type columns

my jQuery looks something like the following:

$('.CalendarDiv').click(function(event)
{
   alert( this.id );
   $(this).next().toggle('slow');
}

What I am trying to accomplish is to set the visiblity of the div tag on/off when a user clicks on the button inside the EditItemTemplate.  Problem is that this function is being called multiple times.  The more records I have, the more times this function is called.  Makes sense as I have many buttons that has the "CalendarDiv" class applied to them.  How can I make this function call only once?

thank you.
0
Comment
Question by:yechan
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 500 total points
ID: 35140969
Change your code to:

$('.CalendarDiv').click(function()
{
   alert( this.id );
   $(this).next().toggle('slow');
});
0
 
LVL 13

Expert Comment

by:devlab2012
ID: 35140996
Where have you written the jquery code on your page? I think you have that code somewhere in the GridView markup. Try using the following code in the <head> section of the page.

$(document).ready(function()
{
  $('.CalendarDiv').click(function()
  {
     alert( this.id );
     $(this).next().toggle('slow');
  });
});
0
 
LVL 1

Author Comment

by:yechan
ID: 35142535
Hey devLab,

the query itself is embedded inside a seperate *.js file.  The GridView is a user control which is eventually embedded/added to a web part page.  Do you think that makes a difference?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now