您的位置:首页 > 其它

DevExpress Exception Solution - The target "X" for the callback could not be found or did not implement ICallbackEventHandler

2010-07-07 15:20 2556 查看

Description

I've loaded a control dynamically and got the "The target "X" for the callback could not be found or did not implement ICallbackEventHandler" error for the control itself or for one of its child controls. Why?

Solution

When the control is loaded dynamically, it should meet the following requirements:

1) It should be loaded on each request.
2) If this control has child controls, it should have a static ID.

If the first condition is ignored, then the control may be lost after a callback or postback. So, the best practice is to load this control on each Page_Load.
If it is required to load it after some action only (for example, after a button is clicked), then a possible solution is to use a session or one of the session alternatives . Here's a simple example:

[C#]
protected void Page_Load(object sender, EventArgs e)
{
if (Session["isControlLoaded"] != null)
{
AddControl();
}
}

protected void ASPxButton1_Click(object sender, EventArgs e)
{
AddControl();
Session["isControlLoaded"] = "loaded";
}

public void AddControl()
{
Control control = LoadControl("MyControl.ascx");
control.ID = "MyControl";
form1.Controls.Add (control);
}


[VB.NET]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (Session("isControlLoaded") is Nothing)
AddControl();
End If
End Sub

Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ASPxButton1.Click
AddControl()
Session("isControlLoaded") = "loaded"
End Sub

Public Sub AddControl()
Dim control As Control = LoadControl("MyControl.ascx")
control.ID = "MyControl"
form1.Controls.Add (control)
End Sub

The second requirement is imposed for the reason that all child controls in the NamingContainer have IDs, which are based on the ID of this container.
For example, if there is an ASPxGridView within the MyControl.ascx (see the previous example) with the static ID = "ASPxGridView1", then its final ID will look like this:

MyControl_ASPxGridView1

If you do not set the static ID for MyControl, then it will be assigned dynamically and changed in each request. Accordingly, the id of the grid will be also changed and this fact may cause the situation when the control someID_ASPxGridView1 that sent a callback is "disappear" because it got a new ID during a request.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐