This is a migrated thread and some comments may be shown as answers.

ajaxRequest from User Control doesn't update embedded control

2 Answers 152 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Francois MARTIN
Top achievements
Rank 2
Francois MARTIN asked on 22 Apr 2008, 02:25 PM
A simple WebForm contains a user control with a RadAjaxPanel.
The user control contains a button that is supposed to fire the ajaxRequest method of the RadAjaxPanel and update a simple label... but it doesn't!

The function ajaxRequest is indeed called, but the label is never updated.

Source code:

Test.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %> 
 
<%@ Register src="AjaxControl.ascx" tagname="AjaxControl" tagprefix="uc1" %> 
 
<!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>Test</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
        <asp:Label ID="Label1" runat="server" Text="Click on the button..."></asp:Label><br /> 
        <uc1:AjaxControl ID="AjaxControl1" runat="server" /> 
          
    </div> 
    </form> 
</body> 
</html> 
 

AjaxControl.aspx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AjaxControl.ascx.cs" Inherits="AjaxControl" %> 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
    <script type="text/javascript">  
    function InitiateAsyncRequest(argument)  
    {  
        var ajaxPanel = $find("<%=RadAjaxPanel1.ClientID%>");  
 
        ajaxPanel.ajaxRequest(argument);  
    }  
    </script> 
</telerik:RadCodeBlock> 
 
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="InitiateAsyncRequest('New Text')" /> 
<telerik:RadAjaxPanel ID="RadAjaxPanel1" 
    runat="server" Height="200px" Width="300px" OnAjaxRequest="RadAjaxPanel1_AjaxRequest">  
    <asp:Label ID="Label1" runat="server" Text="...and this text will change!"></asp:Label> 
</telerik:RadAjaxPanel> 
 

AjaxControl.aspx.cs:

using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;  
 
public partial class AjaxControl : System.Web.UI.UserControl  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
 
    protected void RadAjaxPanel1_AjaxRequest(object source, Telerik.Web.UI.AjaxRequestEventArgs e)  
    {  
        Label1.Text = e.Argument;  
    }  
}  
 

2 Answers, 1 is accepted

Sort by
0
Accepted
Steve
Telerik team
answered on 22 Apr 2008, 03:35 PM
Hi Francois,

Haven't you noticed that clicking the button causes postback rather than ajax request? The problem is coming from the default behavior of the asp:Buttons which always submit the form, unless you return false to override this behavior. Change the OnClientClick handler like this:

OnClientClick="InitiateAsyncRequest('New Text'); return false;"

Regards,
Steve
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Francois MARTIN
Top achievements
Rank 2
answered on 22 Apr 2008, 03:51 PM
Oops. You are right of course...
Thank you!
Tags
Ajax
Asked by
Francois MARTIN
Top achievements
Rank 2
Answers by
Steve
Telerik team
Francois MARTIN
Top achievements
Rank 2
Share this question
or