D
Dave Weeden
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 ---
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 ---