Newbie: AJAX not working

M

Mark B

This is my first try at using AJAX. I want the calendars to be enabled if
the user checks CheckBox1. It works OK for a normal all page refresh but
once I introduced the AJAX code it stopped working. Any ideas?


<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="default-ajax.aspx.vb" Inherits="pages_verify_groups_Default"
Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 950px;
}
.style2
{
height: 36px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<p>
&nbsp;</p>
<table align="center" class="style1" width="950">
<tr>
<td>
Account</td>
</tr>
<tr>
<td>
Navigation Bar</td>
</tr>
<tr>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
<h2>
My Account</h2>
</td>
</tr>
<tr>
<td height="0">
<h3>
Settings</h3>
</td>
</tr>
<tr>
<td height="0">
<h4>
&nbsp;</h4>
</td>
</tr>
<tr>
<td>
<table>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>


<tr>
<td colspan="4" class="style2">
<asp:CheckBox ID="CheckBox1" runat="server"
Text=" Activate holiday range" />
</td>
</tr>

<asp:UpdatePanel ID="UpdatePanel1"
runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CheckBox1"
EventName="CheckedChanged"/>
</Triggers>

<ContentTemplate>
<tr>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</td>
<td>
Range from:</td>
<td>
&nbsp;&nbsp; &nbsp;</td>
<td>
To:</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Calendar ID="Calendar1" runat="server"
BackColor="White"
BorderColor="Black" BorderStyle="Solid"
CellSpacing="1" Enabled="False"
Font-Names="Verdana" Font-Size="9pt"
ForeColor="Black" Height="250px"
NextPrevFormat="ShortMonth" Width="330px">
<SelectedDayStyle BackColor="#333399"
ForeColor="White" />
<TodayDayStyle BackColor="#999999"
ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True"
Font-Size="8pt" ForeColor="White" />
<DayHeaderStyle Font-Bold="True"
Font-Size="8pt" ForeColor="#333333"
Height="8pt" />
<TitleStyle BackColor="#333399"
BorderStyle="Solid" Font-Bold="True"
Font-Size="12pt" ForeColor="White"
Height="12pt" />
</asp:Calendar>
</td>
<td>
&nbsp;</td>
<td>
<asp:Calendar ID="Calendar2" runat="server"
BackColor="White"
BorderColor="Black" BorderStyle="Solid"
CellSpacing="1" Enabled="False"
Font-Names="Verdana" Font-Size="9pt"
ForeColor="Black" Height="250px"
NextPrevFormat="ShortMonth" Width="330px">
<SelectedDayStyle BackColor="#333399"
ForeColor="White" />
<TodayDayStyle BackColor="#999999"
ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True"
Font-Size="8pt" ForeColor="White" />
<DayHeaderStyle Font-Bold="True"
Font-Size="8pt" ForeColor="#333333"
Height="8pt" />
<TitleStyle BackColor="#333399"
BorderStyle="Solid" Font-Bold="True"
Font-Size="12pt" ForeColor="White"
Height="12pt" />
</asp:Calendar>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td colspan="4">
<asp:CheckBox ID="CheckBox2" runat="server"
Text=" Comments" AutoPostBack="True" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style3" colspan="3">
<asp:TextBox ID="TextBox1" runat="server"
Height="181px"
TextMode="MultiLine" Width="686px"
Enabled="False"></asp:TextBox>
</td>
</tr>

</ContentTemplate>
</asp:UpdatePanel>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>


Imports System.Data.SqlClient
Imports System.Data

Partial Class pages_verify_groups_Default
Inherits System.Web.UI.Page


Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles CheckBox1.CheckedChanged
fEnableMode()
End Sub

Function fEnableMode() As Boolean

'Enable or disable controls
Select Case CheckBox1.Checked
Case True
Calendar1.Enabled = True
Calendar2.Enabled = True
CheckBox2.Enabled = True
Case Else
Calendar1.Enabled = False
Calendar2.Enabled = False
CheckBox2.Enabled = False
End Select

TextBox1.Enabled = True
If CheckBox2.Enabled = False Or CheckBox2.Checked = False Then
TextBox1.Enabled = False
End If

End Function


Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles CheckBox2.CheckedChanged
fEnableMode()
End Sub

Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Calendar1.SelectionChanged
fCalendarChange()
End Sub

Function fCalendarChange() As Boolean
Dim strStandardOutOfOfficeText As String
Dim strTo As String
Dim strEnd As String
strStandardOutOfOfficeText = "I am on holiday from "
strTo = " to "
strEnd = "."

Dim strNewOutOfOfficeText As String
'If Left(TextBox1.Text, Len(strStandardOutOfOfficeText)) =
strStandardOutOfOfficeText Or TextBox1.Text = "" Then
strNewOutOfOfficeText = strStandardOutOfOfficeText +
Calendar1.SelectedDate.ToLongDateString + strTo +
Calendar2.SelectedDate.ToLongDateString
TextBox1.Text = strNewOutOfOfficeText + Mid(TextBox1.Text,
Len(strNewOutOfOfficeText)) + strEnd
'End If

End Function

Protected Sub Calendar2_SelectionChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Calendar2.SelectionChanged
fCalendarChange()
End Sub
End Class
 
S

Steven Cheng [MSFT]

Hi Mark,

Based on your description, I've performed some test on my side. It seems
the CheckBox and Calender can work correctly with AJAX page(via
UpdatePanel). here is a very simple test page I've used.

==============aspx====================
<body>
<form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1"
runat="server" />

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
Checked="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CheckBox1"
EventName="CheckedChanged" />
</Triggers>
<ContentTemplate>

<br />
<br />
<br />
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
<br />
</ContentTemplate>
</asp:UpdatePanel>

<div>
</div>
</form>
</body>


=============code behind================

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles CheckBox1.CheckedChanged
Calendar1.Enabled = CheckBox1.Checked

End Sub
======================================

I think the problem might be the ASP.NET AJAX application is not configured
correctly. Have you tried some other kind of AJAX postback(via updatepanel)
to see whether it works? Here is the web.config content of my test project,
you can also compare it with yours to see whether any configuration section
is different:

<<<<<<<<<< web.config <<<<<<<<
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions"
type="System.Web.Configuration.SystemWebExtensionsSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting"
type="System.Web.Configuration.ScriptingSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler"
type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="false"
allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices"
type="System.Web.Configuration.ScriptingWebServicesSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization"
type="System.Web.Configuration.ScriptingJsonSerializationSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="false"
allowDefinition="Everywhere" />
<section name="profileService"
type="System.Web.Configuration.ScriptingProfileServiceSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="false"
allowDefinition="MachineToApplication" />
<section name="authenticationService"
type="System.Web.Configuration.ScriptingAuthenticationServiceSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" requirePermission="false"
allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>

<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI"
assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="false">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd"
type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
validate="false"/>
</httpHandlers>

<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>

<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom
converter -->
<!--
<jsonSerialization maxJsonLength="500">
<converters>
<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
</converters>
</jsonSerialization>
-->
<!-- Uncomment this line to enable the authentication service.
Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "true|false"/>
-->

<!-- Uncomment these lines to enable the profile service. To allow
profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each
property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true"
/>
-->
</scripting>
</system.web.extensions>

<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx"
preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*"
path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode"
verb="GET,HEAD" path="ScriptResource.axd"
type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>
</configuration>

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top