0

I have made a very simple sample project where I want to toggle an asp .net calendar control through jquery. Could anyone please point out why it is not working. I have made no changes to master page from the sample project provided for ASP .NET web application.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"     AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="scripts/jquery-1.4.1.min.js">
</script>
<script language="javascript" type="text/javascript">
// <![CDATA[

    function Button1_onclick() {
       alert( $('<%=Calendar1.ClientID%>'));
        $('<%=Calendar1.ClientID%>').toggle();
    }

// ]]>
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Welcome to ASP.NET!
</h2>
<p>
    To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET     Website">www.asp.net</a>.
</p>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?    LinkID=152368&amp;clcid=0x409"
        title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>
6
  • what type of error you getting?? Commented Oct 29, 2013 at 9:32
  • First thing: Why are you using such an old version of jQuery? Commented Oct 29, 2013 at 9:33
  • It just does not do anything on button click. Only shows the alert. Commented Oct 29, 2013 at 9:35
  • Sorry I am a bit new to jqyer. The jquery version is the one automatically added in the Visual studio project. Which version should I use? Will that solve the problem? Commented Oct 29, 2013 at 9:36
  • It won't solve your problem, but it's always a good idea to use a newer version, as you're starting a new project anyway. Commented Oct 29, 2013 at 9:54

4 Answers 4

2

To select an element by its ID on jQuery, you need to precede it with a hash (#):

function Button1_onclick() {
    $('#<%=Calendar1.ClientID%>').toggle();
}

Or, if you don't want to rely on the element ID, give it a class and do it this way:

<asp:Calendar ID="Calendar1" runat="server" CssClass="myCalendar"></asp:Calendar> 

function Button1_onclick() {
    $('.myCalendar').toggle();
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think You are not getting CalendarControl's ID suing JQuery, In Jquery, if you want to access control then it can be like $('#Calendar1').toggle(); Try this one

1 Comment

i suggested the same but its not the answer!!
0

jQuery id selector starts with #

function Button1_onclick() {
   alert( $('#<%=Calendar1.ClientID%>'));
    $('#<%=Calendar1.ClientID%>').toggle();
}

Comments

-1
try this..



function Button1_onclick() {
           alert( $('#<%= Calendar1.ClientID %>').val());
           $('#<%= Calendar1.ClientID %>').toggle();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.