[CLOSED] UserControlLoader handling events define in User Control

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    [CLOSED] UserControlLoader handling events define in User Control

    Since moving the UserControl into a UserControlLoader, my events are not firing. I have created a sample as best I can to show the issue. Basically my UserControl raises an event that the containing page handles, which worked prior to moving it into a UserControlLoader for the issue linked below. I think the problem is basically before I had the actual user control defined in the page and now I just have a reference to the UserControlLoader and not the actual UserControl. Just not sure how to define the handler for the event in the containing page.

    This is a result of having to use UserControlLoader instead of a defined user control inside of a Content tag, see:
    http://forums.ext.net/showthread.php...a-user-control

    UserControl
    <%@ Control Language="vb"%>
    
    <script runat="server">
    
        Partial Public Class QSearch
            Inherits System.Web.UI.UserControl
    
            Public Event Selected As EventHandler
    
            Public Class QuickSearchEventArgs
                Inherits Ext.Net.DirectEventArgs
    
                Public SelectedEntity As String
    
                Public Sub New(extraParams As Ext.Net.ParameterCollection)
                    MyBase.New(extraParams)
                End Sub
    
            End Class
    
            Public Sub Results_Select(ByVal sender As Object, ByVal e As Ext.Net.DirectEventArgs)
                Dim value = e.ExtraParams("Key")
                Dim params As New Ext.Net.ParameterCollection()
                params.Add(New Ext.Net.Parameter("SameWindow", True))
                Dim args As New QuickSearchEventArgs(params) With {
                        .SelectedEntity = "x"
                        }
                RaiseEvent Selected(Me, args)
            End Sub
    
        End Class
    </script>
    
    <ext2:ComboBox 
        ID="Results" 
        runat="server" 
        ValueField="Key"
        TypeAhead="false" 
        LoadingText="Searching..."
        HideTrigger="true" 
        EmptyText="Type to Search" 
        QueryDelay="1500" 
        QueryCaching="false"
        MinChars="1" 
        Border="false"
        Width="920" 
        Height="38">
    
        <Store>
            <ext2:Store ID="Store1" runat="server">
                <Model>
                    <ext2:Model runat="server" IDProperty="Value">
                        <Fields>
                            <ext2:ModelField Name="Text" />
                            <ext2:ModelField Name="Value" />
                        </Fields>
                    </ext2:Model>
                </Model>
            </ext2:Store>
        </Store>
        <Items>
            <ext2:ListItem Text="Text1" Value="1" />
            <ext2:ListItem Text="Text2" Value="2" />
            <ext2:ListItem Text="Text3" Value="3" />
        </Items>
        <DirectEvents>
            <Select OnEvent="Results_Select">
                <EventMask ShowMask="true" Target="Page" />
                <ExtraParams>
                    <ext2:Parameter Name="Key" Value="testing" Mode="Raw" />
                </ExtraParams>
            </Select>
        </DirectEvents>
     </ext2:ComboBox>
    Main page
    <%@ Page Language="vb" %>
    
     <script runat="server">
    
         Protected WithEvents QuickSearch As Qsearch???
         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         End Sub
         Private Sub QuickSearch_Selected(sender As Object, e As QSearch.QuickSearchEventArgs) Handles QuickSearch.Selected
    
             Ext.Net.X.AddScript("alert('test');")
         End Sub
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Simple Combo</title>
    </head>
    <body>
     
        <ext2:ResourceManager ID="ResourceManager" runat="server" />
    
        <ext2:Window ID="Window1" runat="server" Visible="true" Maximized="true" Padding="5" Resizable="true" MinHeight="700" MinWidth="500" Closable="false">
            <TopBar>
                <ext2:Toolbar ID="Toolbar1" runat="server">
                    <Items>
    
                        <ext2:Button ID="QuickLaunchReport" runat="server" Text="This does not work" >
                            <Menu>
                                <ext2:Menu ID="QuickLaunchReportMenu" runat="server" RenderToForm="true" Width="450" ShowSeparator="false">
                                    <Items>
                                        <ext2:Panel runat="server">
                                            <Items>
                                                <ext2:UserControlLoader runat="server" ID="UCQuickSearch" UserControlID="QuickSearch" Path="QSearch.ascx"></ext2:UserControlLoader>
                                            </Items>
                                        </ext2:Panel>
                                    </Items>
                                </ext2:Menu>
                            </Menu>
                        </ext2:Button>
    
                     </Items>
                </ext2:Toolbar>
            </TopBar>
    
            <Items>
                <ext2:Panel ID="pnlSetDefaults" runat="server">
                   <Items>
      
                   </Items>
                </ext2:Panel>
            </Items>
        </ext2:Window>
    
    </body>
    </html>
    Last edited by fabricio.murta; Feb 10, 2017 at 8:12 PM.
  2. #2
    Hello @rmelancon!

    Your code seems simple enough, thanks!

    But I'm getting errors on the lines 5 and 8 of your "main page" code. Type 'Qsearch' is not defined and QSearch.QuickSearchEventArgs, respectively.
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Well that's kind of the problem... when the control is added with a directive and content tag, ie:
    <%@ Register TagPrefix="uc" TagName="test" Src="QSearch.ascx" %>
            <Content>
                <uc:test ID="QuickSearch" runat="server" />
            </Content>
    I can create the event handler with:

    Private Sub QuickSearch_Selected(sender As Object, e As QSearch.QuickSearchEventArgs) Handles QuickSearch.Selected
        Ext.Net.X.AddScript("alert('test');")
    End Sub
    Which works because the control is defined within the page (in designer.vb file) as:
    Protected WithEvents QuickSearch As Global.Qsearch
    Now that I have to use the UserControlLoader to get around the bug referenced above, I no longer have a reference to the control I'm loading, just the UserControlLoader. So I'm trying to figure out how to add a handler to the control loaded with UserControlLoader.

    Is this making sense?
  4. #4
    Got it, Qsearch was meant to be a reference to the component in the control. But to me the code you defined as the page does not mention "Qsearch" as the ID of anything at any moment. So that's not going to make it to the designer nor to the code behind in any way.

    I'll give another look at it with this information and will see what can be done in regards to it. At first, it looks like the component will have to be caught indirectly as instructed in this post.

    Will post a follow-up here soon.
    Fabrício Murta
    Developer & Support Expert
  5. #5
    Hello again!

    Another consideration on your sample code:

    The user control code does not work when I try to use it via the normal user control mechanism (thru the Register Src tag). You defined a sub-class in the script runat="server" block. That block is automatically wrapped in the own page class name. So you created something like My_Webform_Page.QSearch (sub-class of the web forms page). This results in the event not being located at all. The Inherits part is also not supported. The server side script block should look like something like this:

    <script runat="server">
        Public Event Selected As EventHandler
        (...)
    </script>
    To this point, I could get the Results_Select() event (your control code, line 21) to be triggered using it with ext:UserControlLoader.

    But it is still not clear to me how you want to bind the QuickSearch_Selected() event to the combo box.

    Can you review your example like the other thread, and leave a copy of the component the way it was working for you? You may need to fix your user control code and the main page code, but I think no second user control nor anything else is necessary.

    I am not sure how line 5 of your main page code should "see" the user control class, for example, if there's no designer.vb nor .aspx.vb, but script runat="server" in the page..
    Fabrício Murta
    Developer & Support Expert
  6. #6
    Thanks, I will give those a try. Sometimes it is hard to strip out an example from a very complex page with code behind etc. to show the problem and that's what I'm running into here. I will let you know, thanks.

Similar Threads

  1. Code behind content handling (from client events)
    By fabricio.murta in forum 2.x Help
    Replies: 5
    Last Post: Oct 22, 2014, 6:26 AM
  2. Replies: 1
    Last Post: Sep 28, 2014, 7:36 AM
  3. Replies: 3
    Last Post: Sep 05, 2013, 8:18 AM
  4. Replies: 1
    Last Post: Jan 28, 2013, 5:26 AM
  5. [CLOSED] Direct events in user control
    By tiramisu in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Dec 13, 2010, 11:34 AM

Posting Permissions