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
Solved

jQuery and GridView question

Posted on 2011-03-15
3
430 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 36
HTML Anchor Link Problem 3 35
Using toggleClass to resize Bootstrap columns 7 15
asp.net mvc 2 20
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

789 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