views:

315

answers:

2

I have my own Control1 which is dynamically added as child control to Control2 which implements INamingContainer in CreateChildControls() of control2.

Control1 itself implements IPostBackEventHandler. But RaisePostBackEvent() method is never called on Control1, despite I do call postback method from JavaScript.

And yes, there are other controls which implement IPostBackEventHandler interface on the page.

What did I miss?

What could cause the issue?

UPDATE: Control1 is always created exactly the same way and assigned exactly the same ID in Control2

it looks like this in Control2:

protected override void CreateChildControls()
{
  if(!this.DesignMode)
  {
    Control1 c = new Control1();
    c.ID = "FIXED_ID";
  }
  base.CreateChildControls();
}

UPDATE2: Control1:

public class Control1: Control, IPostBackEventHandler
{
...
    protected virtual void RaisePostBackEvent(string eventArgument)
    {
              if(!String.IsNullOrEmpty(eventArgument))
              {
                  // Some other code
              }
    }       
}

if I add line

Page.RegisterRequiresRaiseEvent(c);

In CreateChildControls() in Control2 then this method is being called but always with null eventArgument.

UPDATE3:

In JavaScript on some onClick event I do the following:

__doPostBack(Control1.UniqueID,'commandId=MyCommand');

where Control1.UniqueID is of course substituted with real uniqueID during rendering. I checked, this script is being called.

A: 

I'm going to guess that it's the "dynamically added as child control to Control2" that is the issue, but without any code it's pretty hard to diagnose.

When during the page lifecycle are you dynamically adding it? Are you recreating the dynamic control in the exact same way, with the same ID, after the postback?

womp
Did I answer your question?
Artem
+1  A: 

Can you show us the source code of first control? Anyway there is a simple example.

public class TestControl2 : CompositeControl
{

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        if (!DesignMode)
            this.Controls.Add(new TestControl());
    }

}

public class TestControl : WebControl, IPostBackEventHandler
{

    public TestControl() : base(HtmlTextWriterTag.Input) { }

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);

        writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
        writer.AddAttribute(HtmlTextWriterAttribute.Name, base.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, null));
        writer.AddAttribute(HtmlTextWriterAttribute.Value, "Submit Query");
    }

    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        // Raise post back event
    }

}

Edit

  1. Why you are generating the post back script out of the control and manually? You have to use Page.ClientScript.GetPostBackEventReference method. It generates and includes some necessary inline and embedded scripts to the page.

  2. Why you are deriving your class from Control? It's good for those controls which don't have any user interface.

From MSDN

This is the primary class that you derive from when you develop custom ASP.NET server controls. Control does not have any user interface (UI) specific features. If you are authoring a control that does not have a UI, or combines other controls that render their own UI, derive from Control. If you are authoring a control that does have a UI, derive from WebControl or any control in the System.Web.UI.WebControls namespace that provides an appropriate starting point for your custom control.

You have to derive your control from WebControl class as follows.

public class TestCtl : WebControl, IPostBackEventHandler
{

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        // Add onclick event.
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Arguments"));
    }

    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        throw new NotImplementedException();
    }

}
Mehdi Golchin
I don't have an input, it just a control which reacts on some onclick() event.
Artem
Sorry for delay. I have updated my post, check it out.
Mehdi Golchin