AJAX.NET Asnycpostback not working

294 Views Asked by At

I created an AJAX.NET application and I am running my application with the help of <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> but now my following sample code is posting back on every button click. I need the action to be done without reloading the page.

Code follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Async="true" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Hello";
    }
}
5

There are 5 best solutions below

1
On BEST ANSWER

I found the solution in below way

  1. Install Ajax tool kit.
  2. Add public key and version number to web.config file.
  3. Copy and paste Ajax dll files to bin folder of required data folder.
3
On

Whenever you have issues with this sort of thing, it's best to just create a new project with the same version of .Net and see what it puts in your web.config.

For .Net 3.5, it creates this:

  <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
0
On

I took your example and got it working with the following steps:

Check whether the right assembly is installed on your machine. In your case you will need ASP.NET AJAX 1.0 which can be downloaded here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=883

This will install the System.Web.Extensions 1.0.61025.0 assembly in your GAC.

Reference the assembly in your website.

Check if your web.config has at least the following configuration:

<system.web>
    <compilation debug="true">
        <assemblies>
            <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </assemblies>
    </compilation>

    <pages>
       <controls>
           <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
       </controls>
    </pages>

    <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="GET" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler" validate="false"/>      
    </httpHandlers> 

     other stuff

</system.web>

The code behind of my page is:

public partial class WebForm1 : System.Web.UI.Page
{        
    protected void Page_Load(object sender, EventArgs e)
    {            
    }

    protected void button1_click(object sender, EventArgs e)
    {
        Label1.Text = "Hello";
    }
}

The designer file of my page is:

public partial class WebForm1 {        
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;       
    protected global::System.Web.UI.ScriptManager ScriptManager1;       
    protected global::System.Web.UI.UpdatePanel UpdatePanel1;       
    protected global::System.Web.UI.WebControls.Label Label1;      
    protected global::System.Web.UI.WebControls.Button Button1;
}

That's all. As Yuriy Rozhovetskiy said: the executing of the page_load is normal behaviour if you click on the button!

0
On

I'm assuming that you must have everything installed correctly since you're site is running. If there was something wrong with your web.config values, or if you didn't have the AJAX Toolkit installed, your code would bomb out.

Now, you said that when you click the button the page is reloading, and I assume by that you mean that the page is doing a full postback.

To narrow down the list of possibilities, here are a few things to try:

  1. Since the example contains only a Label and a Button, you shouldn't need to specify any triggers in the UpdatePanel. Without specifying any triggers, the UpdatePanel will assume that everything contained within will use AJAX;
  2. In the script manager, set EnablePartialRendering to true
  3. I noticed in your example that the file name does not match the code-behind you're inheriting from. This shouldn't really make a difference, but I would change the class declaration to reflect the file name in the code-behind, just to make sure.
  4. You are using the CodeFile property in the page directive. You should be using the CodeBehind property instead, as this is what's used in the later versions of ASP.NET.

Here is a test case that works as expected:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="_Default" %>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
    </ContentTemplate> 
</asp:UpdatePanel>

Code-behind:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "testing";
}
0
On

Please download ajax control toolkit from here..

and register it on top of aspx page as below <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

using tagprefix add your ajax control and test it again and let me know your results.

Thanks Arun.