DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Mon, 20 Aug 2007 12:52:00 -0700,    posted on: microsoft.public.dotnet.framework.aspnet.buildingcontrols        back       

Thread Index
  1    Dave Weeden am
          2    (Walter Wang [MSFT])
                 3    (Walter Wang [MSFT])
                        4    Dave Weeden am
                        5    (Walter Wang [MSFT])


Controls not updated by callback event   
Hi all,

We have developed a custom web control that implements ICallbackEventHandler 
and fires an event when the callback is invoked.

We had hoped that subscribers to this event would be able to update the 
state of other web controls but have been unable to make this work.

I have distilled the control down to the minimum required to illustrate the 
issue and have shown a minimal consumer web form (both are inline below).

In the web form, both Button1_Click and Wrapper1_Save are called (verified 
through breakpoints) but the latter does not update the label properly. 

Is this a limitation of the modified callback lifecycle or is there some way 
to make it work?

Thanks,

Dave


--- start of control ---

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyControls
{
    [ToolboxData("<{0}:MyControl runat=\"server\"></{0}:MyControl>")]
    public class MyControl : WebControl, ICallbackEventHandler
    {
        public event EventHandler MyEvent;

        private string result;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            Page.ClientScript.RegisterClientScriptBlock(typeof(MyControl), 
"OnSuccess", "function OnSuccess(result, context) {}", true);
            
            string callback = 
Page.ClientScript.GetCallbackEventReference(this, "result", "OnSuccess", 
"context");
            
            Page.ClientScript.RegisterClientScriptBlock(typeof(MyControl), 
ID, string.Concat("function ", ID, "Callback(result, context) { ", callback, 
"; }"), true);
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            result = eventArgument;

            if (MyEvent != null)
            {
                MyEvent(this, EventArgs.Empty);
            }
        }

        public string GetCallbackResult()
        {
            return result;
        }

    }
}

--- end of control ---

--- start of web page ---

<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="MyControls" %>

<script runat="server">
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "PostBack button clicked";
    }
    
    protected void Wrapper1_Save(object sender, EventArgs e)
    {
        Label1.Text = "CallBack button clicked";
    }
    
</script>

<html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <custom:MyControl ID="MyControl1" OnMyEvent="Wrapper1_Save" 
runat="server" />
            <asp:Button ID="Button1" Text="PostBack" runat="server" 
OnClick="Button1_Click" />
            <asp:Button ID="Button2" Text="CallBack" 
OnClientClick="MyControl1Callback('Testing123', null)" runat="server" />
            <asp:Label ID="Label1" runat="server" />
        </div>
        </form>
    </body>
</html>

--- end of web page  ---
Date:Mon, 20 Aug 2007 12:52:00 -0700   Author:  

RE: Controls not updated by callback event   
Hi Dave,

In a client callback, a client script function sends a request to the 
ASP.NET Web page, which then runs an abbreviated version of its normal life 
cycle to process the callback: the page is initiated and its controls and 
other members are created, and then a specially marked method is invoked. 
The method performs the processing that you have coded and then returns a 
value to the browser that can be read by another client script function. 

Besides implementing ICallbackEventHandler and its methods 
RaiseCallbackEvent, GetCallbackResult, the page must contain three client 
script functions that perform the following actions:

 * One function calls a helper method that performs the actual request to 
the server. In this function, you can perform custom logic to prepare the 
event arguments first, and then you can send a string as a parameter to the 
server-side callback event handler.
 * Another function receives (is called by) the result from the server code 
that processed the callback event, accepting a string that represents the 
results. This is called the client callback function.
 * A third function is the helper function that performs the actual request 
to the server, which is generated automatically by ASP.NET when you 
generate a reference to this function using the GetCallbackEventReference 
method in server code.


The key point here is that it's not a postback, the page will not be 
rendered again; if you change the server-side controls' properties, the 
changes will not be rendered. 

Therefore, in your code, if we make following changes:

(In MyControl)

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            
//Page.ClientScript.RegisterClientScriptBlock(typeof(MyControl), 
"OnSuccess", "function OnSuccess(result, context) {     }", true);

            string callback = 
Page.ClientScript.GetCallbackEventReference(this, "result", 
string.Concat(ID, "OnSuccess"), "context");

            Page.ClientScript.RegisterClientScriptBlock(typeof(MyControl), 
ID, string.Concat("function ", ID, "Callback(result, context) { ", 
callback, "; }"), true);
        }


(In ASPX)

<head>
    <title>Test Page</title>
    
    <script language="javascript" type="text/javascript">
    function MyControl1OnSuccess(text) {
    alert(text);
    }
    </script>
</head>



References:

#Implementing Client Callbacks Without Postbacks in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/library/ms178208.aspx

#Client-Callback Implementation (C#) Example
http://msdn2.microsoft.com/en-us/library/ms178210.aspx



Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this 
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure 
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to 
see your reply promptly.

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.
Date:Tue, 21 Aug 2007 07:02:49 GMT   Author:  

RE: Controls not updated by callback event   
Hi Dave,

Some updates:

<html>
<head>
    <title>Test Page</title>
    
    <script language="javascript" type="text/javascript">
    function MyControl1OnSuccess(text) {
        //alert(text);
        document.getElementById("Label1").innerHTML = text;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <custom:MyControl ID="MyControl1" OnMyEvent="Wrapper1_Save" 
runat="server" />
            <asp:Button ID="Button1" Text="PostBack" runat="server" 
OnClick="Button1_Click" />
            <asp:Button ID="Button2" Text="CallBack" 
OnClientClick="MyControl1Callback('Testing123', null); return false;"
                runat="server" />
            <asp:Label ID="Label1" runat="server" />
        </div>
    </form>
</body>
</html>


1) To update the label's content on callback, use DHTML dom elements to 
change its content.

2) You need to add "return false" in button2's OnClientClick, otherwise 
there's an additional normal postback at the end (which will re-render the 
page content according to server-side controls' states -- which means the 
label1's content that we changed via script callback will be overwritten 
again).


Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Date:Tue, 21 Aug 2007 07:18:38 GMT   Author:  

RE: Controls not updated by callback event   
Hey Walter,

Thanks for that very good explanation.  We are in fact trying to wrap some 
AJAX type behaviour in a control and wanted to insulate the consumer of the 
wrapper from having to code any javascript.

Looks like that is not possible so we'll be using something similar to what 
you have suggested.

Thanks again!

Dave

""Walter Wang [MSFT]"" wrote:


> Hi Dave,
> 
> Some updates:
> 
> <html>
> <head>
>     <title>Test Page</title>
>     
>     <script language="javascript" type="text/javascript">
>     function MyControl1OnSuccess(text) {
>         //alert(text);
>         document.getElementById("Label1").innerHTML = text;
>     }
>     </script>
> </head>
> <body>
>     <form id="form1" runat="server">
>         <div>
>             <custom:MyControl ID="MyControl1" OnMyEvent="Wrapper1_Save" 
> runat="server" />
>             <asp:Button ID="Button1" Text="PostBack" runat="server" 
> OnClick="Button1_Click" />
>             <asp:Button ID="Button2" Text="CallBack" 
> OnClientClick="MyControl1Callback('Testing123', null); return false;"
>                 runat="server" />
>             <asp:Label ID="Label1" runat="server" />
>         </div>
>     </form>
> </body>
> </html>
> 
> 
> 1) To update the label's content on callback, use DHTML dom elements to 
> change its content.
> 
> 2) You need to add "return false" in button2's OnClientClick, otherwise 
> there's an additional normal postback at the end (which will re-render the 
> page content according to server-side controls' states -- which means the 
> label1's content that we changed via script callback will be overwritten 
> again).
> 
> 
> Hope this helps.
> 
> 
> Regards,
> Walter Wang (wawang@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
> 
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> 
> This posting is provided "AS IS" with no warranties, and confers no rights. 
> 
> 
Date:Tue, 21 Aug 2007 12:26:01 -0700   Author:  

RE: Controls not updated by callback event   
Hi Dave,

I'm glad that the reply helped.

Thank you for using MSDN Managed Newsgroup support service!

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Date:Wed, 22 Aug 2007 07:21:50 GMT   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us