Controls not updated by callback event

Discussion in 'ASP .Net Building Controls' started by Dave Weeden, Aug 20, 2007.

  1. Dave Weeden

    Dave Weeden Guest

    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 ---
    Dave Weeden, Aug 20, 2007
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Aug 21, 2007
    #2
    1. Advertising

  3. 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 (, 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.
    Walter Wang [MSFT], Aug 21, 2007
    #3
  4. Dave Weeden

    Dave Weeden Guest

    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 (, 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.
    >
    >
    Dave Weeden, Aug 21, 2007
    #4
  5. Hi Dave,

    I'm glad that the reply helped.

    Thank you for using MSDN Managed Newsgroup support service!

    Regards,
    Walter Wang (, 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.
    Walter Wang [MSFT], Aug 22, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelly

    dynamic controls not updated

    Kelly, Jun 25, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    870
    Natty Gur
    Jun 25, 2003
  2. Natty Gur

    Re: dynamic controls not updated

    Natty Gur, Jun 25, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,755
    Kelly Lim
    Jun 26, 2003
  3. Borr
    Replies:
    2
    Views:
    1,639
    bruce barker
    Nov 5, 2003
  4. Randall Parker

    Event that runs once when server site updated?

    Randall Parker, Feb 8, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    304
    Trevor Benedict R
    Feb 8, 2006
  5. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    403
    Dmitry Korolyov
    Sep 22, 2003
Loading...

Share This Page