Wednesday, March 28, 2012

Simple ajax problem?

I'm trying to follow the instructions on
http://www.asp.net/learn/videos/vie...?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
--
<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="SimpleAjax.aspx.cs"
Inherits="Forms_SimpleAjax" %>
<%@dotnet.itags.org. 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> <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)
{
}
}> 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
David R. Longnecker
Web Developer
http://blog.tiredstudent.com

> I'm trying to follow the instructions on
> http://www.asp.net/learn/videos/vie...?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> <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)
> {
> }
> }
Thanks anyway, but that's not it. double checked the tutorial and the autho
r
didn't do that. If you wrap the code, it will only populate the labels the
first time in.
"David Longnecker" wrote:

> 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 co
de
> (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 Pag
e
> Postback (aka: this is your FIRST page load), then populate the labels..."
> HTH.
> -dl
> --
> David R. Longnecker
> Web Developer
> http://blog.tiredstudent.com
>
>
>
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 cod
e:
"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 );
"Dave" wrote:

> I'm trying to follow the instructions on
> http://www.asp.net/learn/videos/vie...?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> <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)
> {
> }
> }
>

0 comments:

Post a Comment