[CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

  1. #1

    [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls



    Hi, I'm trying to wire events for controls that have been created in code behind and I'm finding that they do not fire. If I create the control in markup and wire the event in code everything works as expected. Is there any limitation on event wiring that I should know about before I continue?

    I've included sample below incase I'm missing something obvious.

    C#
        protected void Page_Load(object sender, EventArgs e)
        {
            Ext.Net.Button b = new Ext.Net.Button();
    
            b.ID = "myButton";
            b.Text = "Button";
            this.Form.Controls.Add(b);
            b.Render();
            b.DirectEvents.Click.Event += Button_Click;
        }
    
        private void Button_Click(object sender, DirectEventArgs e)
        {
            
        }
    VB

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim b As New Ext.Net.Button
    
        b.ID = "myButton"
        b.Text = "Button"
        Me.Controls.Add(b)
        b.Render(Me)
        AddHandler b.DirectEvents.Click.Event, AddressOf myClick
    End Sub
    <Ext.Net.DirectMethod()> _
    Public Sub myClick(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)
        Throw New NotImplementedException
    End Sub
    Any help on this would be great.

    Thanks,

    GavinR
  2. #2

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    I used this, and it worked for me:

    Test.aspx
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Test.aspx.vb" Inherits="Test" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">  
        <title>Test</title>
    </head>
     <body>
        <form id="Form1" runat="server">
          <ext:ResourceManager ID="ResourceManager1" runat="server" />          
         
        </form>
    </body>
    </html>

    Test.aspx.vb
    Imports Ext.Net
    
    
    Partial Public Class Test : Inherits System.Web.UI.Page
    
    
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim b As New Ext.Net.Button
    
    
        b.ID = "myButton"
        b.Text = "Button"
        Me.Form.Controls.Add(b)
        AddHandler b.DirectEvents.Click.Event, AddressOf myClick
      End Sub
    
    
    
    
      <Ext.Net.DirectMethod()> _
      Public Sub myClick(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)
        Dim win As New Ext.Net.Window
        win.Width = "300"
        win.Height = "200"
        win.Render(Me.Form)
        win.Show()
        'Throw New NotImplementedException
      End Sub
    
    
    End Class
  3. #3

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    Hi, thanks for the reply. Yes that does work. Unfortunately the scope of my problem is wider than this and I've dumbed down my test app too much. If you now add a second button inside the dynamic window you'll see the problem that I'm really facing.

    See code below:

    
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim b As New Ext.Net.Button
    
        b.ID = "myButton"
        b.Text = "Button"
        me.Form.Controls.Add(b)
        'b.Render(Me)
        
        AddHandler b.DirectEvents.Click.Event, AddressOf myClick
    End Sub
    
    
    <Ext.Net.DirectMethod()> _
    Public Sub myClick(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)
        Dim win As New Ext.Net.Window
        
        win.Width = "300"
        win.Height = "200"
        win.Render(Me.Form)
        
        Dim b As New Ext.Net.Button
        
        b.ID = "yourButton"
        b.Text = "Your Button"
        win.Controls.Add(b)
        b.Render(win)
        
        AddHandler b.DirectEvents.Click.Event, AddressOf myClick
        win.Show()
    End Sub
    Thanks
  4. #4

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    It seems a bug, if you look at the button in the DOM it hasn't any click ajaxListener:

    The following is "undefined":

    Ext.getCmp('yourButton').ajaxListeners.click
    Last edited by geoffrey.mcgill; Jul 05, 2010 at 11:02 PM.
  5. #5

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    Hi,

    I believe the problem is because the "Your Button" Button is not recreated early enough in the second request when it is clicked.


    You either need to add "Your Button" earlier in the page life-cycle, or configure the Button to fire a [DirectMethod] instead of the <Click> DirectEvent.


    Hope this helps.


    Geoffrey McGill
    Founder
  6. #6

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    Hi Geoffrey, thanks for the reply.

    I have to say that I don't really follow your comments. How can the control be created earlier in the page life cycle? The page only knows that the control needs to be created when the original button event is raised. This event doesn't occur until after the page load event. Specifically, which event will allow this idea to work correctly?

    Incidentally I've created a temporary work around that fires a client side event that in turn fires the desired directmethod.

    Thanks in advance for your advice.

    Cheers,

    GavinR
    Last edited by geoffrey.mcgill; Jul 05, 2010 at 11:02 PM.
  7. #7

    RE: [CLOSED] [1.0] DirectEvent wiring not working with code behind/dynamically created controls

    I've created a temporary work around that fires a client side event that in turn fires the desired directmethod.

    Yes, this is the best solution.


    Geoffrey McGill
    Founder

Similar Threads

  1. Replies: 5
    Last Post: Aug 07, 2012, 12:07 AM
  2. saving dynamically-created controls' values
    By Skizzot223 in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2012, 12:54 PM
  3. [CLOSED] How to clean up dynamically created controls?
    By jchau in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Aug 23, 2011, 9:51 AM
  4. [CLOSED] Unable to access dynamically created controls
    By rnachman in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 07, 2011, 5:49 AM
  5. Dynamically created controls cookbooks
    By arodier in forum 1.x Help
    Replies: 15
    Last Post: May 07, 2010, 7:12 PM

Posting Permissions