?
Solved

jQuery and GridView question

Posted on 2011-03-15
3
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
devlab2012 earned 2000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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)
Suggested Courses

777 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