Simple ajax problem?

G

Guest

I'm trying to follow the instructions on
http://www.asp.net/learn/videos/view.aspx?tabid=63&id=75 for a simple AJAX
demo.

However, my code updates all 3 labels even though only 1 of them is inside
an UpdatePanel. What am I not doing that the presenter is?

Here's the my ASPX and codebehind code..

ASPX
------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleAjax.aspx.cs"
Inherits="Forms_SimpleAjax" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:Label ID="label1" runat=server></asp:Label>&nbsp;<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="label2" runat=server></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="label3" runat=server></asp:Label>
<br />
<br />
</div>
</form>
</body>
</html>

Codebehind
--------------

public partial class Forms_SimpleAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
label2.Text = DateTime.Now.ToString();
label3.Text = DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{

}
}
 
D

David Longnecker

protected void Page_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
label2.Text = DateTime.Now.ToString();
label3.Text = DateTime.Now.ToString();
}

Everytime you load your page, you are updating all three buttons again via
the Page_Load event.

I haven't ran through the tutorial, but I bet that if you surround that code
(the three label lines) with:

if (!Page.IsPostBack)
{

-- code here --

}

It'll fix your problem. That code above basically says "If it's NOT a Page
Postback (aka: this is your FIRST page load), then populate the labels..."

HTH.

-dl
 
G

Guest

Thanks anyway, but that's not it. double checked the tutorial and the author
didn't do that. If you wrap the code, it will only populate the labels the
first time in.
 
G

Guest

Just to follow-up on my previous post. This project is converted from
ASP.NET 1.1 which I didn't think would be an issue.

But If I create a new website from scratch using the "ASP.NET AJAX-Enabled
Website" project template, I get a different problem when using the same code:

"Microsoft JScript runtime error: Object doesn't support this property or
method"
in the MicrosoftAjax.js file with references to the following js methods...

return function() {
return method.apply(instance, arguments);
}

and on the line where this is called...

this._xmlHttpRequest.open(verb, this._webRequest.getResolvedUrl(), true );
 

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

Top