[CLOSED] Direct events in user control

  1. #1

    [CLOSED] Direct events in user control

    Hi

    I don't know how to implement the directevents in my user control as follows:

     
    using System;
    using System.Web;
    using Ext.Net;
    namespace BPM.MVC.ViewControls
    {
        public class ThemeMenu : Ext.Net.Menu
        {
            // Change theme
            protected void ChangeTheme(object sender, DirectEventArgs e)
            {
                Theme myTheme = (Theme)Enum.Parse(typeof(Theme), e.ExtraParams["myTheme"]);
                Ext.Net.ResourceManager.GetInstance(HttpContext.Current).SetTheme(myTheme);
                HttpContext.Current.Session["Ext.Net.Theme"] = myTheme;
                //((CheckMenuItem)sender).Checked = true;
            }
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                this.Add(new CheckMenuItem {
                    Text = "Default", 
                    Group = "Theme",
                    Checked = ResourceManager.GetInstance(HttpContext.Current).Theme == Ext.Net.Theme.Default ? true : false
                });
                this.Add(new CheckMenuItem
                {
                    Text = "Gray",
                    Group = "Theme",
                    Checked = ResourceManager.GetInstance(HttpContext.Current).Theme == Ext.Net.Theme.Gray ? true : false
                });
                this.Add(new CheckMenuItem
                {
                    Text = "Slate",
                    Group = "Theme",
                    Checked = ResourceManager.GetInstance(HttpContext.Current).Theme == Ext.Net.Theme.Slate ? true : false
                });
            }
        }
    }
    The original declarative code was as follows:


    <ext:Menu ID="Menu1" runat="server">
        <Items>
            <ext:CheckMenuItem runat="server" ID="myThemeDefault" Text="Default" Group="Theme">
                <DirectEvents>
                    <Click OnEvent="ChangeTheme">
                        <ExtraParams>
                            <ext:Parameter Name="myTheme" Value="Default" />
                        </ExtraParams>
                    </Click>
                </DirectEvents>
            </ext:CheckMenuItem>
            <ext:CheckMenuItem runat="server" ID="myThemeGray" Text="Gray" Group="Theme">
                <DirectEvents>
                    <Click OnEvent="ChangeTheme">
                        <ExtraParams>
                            <ext:Parameter Name="myTheme" Value="Gray" />
                        </ExtraParams>
                    </Click>
                </DirectEvents>
            </ext:CheckMenuItem>
            <ext:CheckMenuItem runat="server" ID="myThemeSlate" Text="Slate" Group="Theme">
                <DirectEvents>
                    <Click OnEvent="ChangeTheme">
                        <ExtraParams>
                            <ext:Parameter Name="myTheme" Value="Slate" />
                        </ExtraParams>
                    </Click>
                </DirectEvents>
            </ext:CheckMenuItem>
        </Items>
    </ext:Menu>
    The control is used in the page as follows (Page excerpt):

     
    <ext:MenuItem runat="server" Text="Theme" Icon="Paintcan">
    <Menu>
    <bpm:ThemeMenu runat="server" />
    </Menu>
    </ext:MenuItem>
    - Actualy i'm not sure if this is the rigth way to encapsule simple functionality used at main level. (Theme change etc.)
    ... (I lost a bit overview when to use which technique. ... "loadcontrol" prefixed taged controls? typed controls? )
    - A further question is how to define the direct events in the class in the top of this post.

    What is your opionon? Your advice on that?
    Last edited by Daniil; Oct 14, 2010 at 6:58 PM. Reason: [CLOSED]
  2. #2
    Hi tiramisu,

    I think adding items to a menu should be moved in the OnLoad event.
    Also DirectEvents should be attached there.
    menuItem.DirectEvents.Click.Event += ChangeTheme;
  3. #3
    Thank you it works ;)
  4. #4

    Adding DirectEvents for Dynamically added Menu Items

    Hi! I am adding some menuitems from serverside on PageLoad event as show below:
    foreach (DataRow dr in dt.Rows)
            {
                MenuItem MI = new MenuItem();
                MI.Text = dr["Name"].ToString();
    
                MI.DirectEvents.Click.Event += MI_Click;
    
                desktop.StartMenu.Items.Add(MI);
            }
    and below is the DirectEvent declaration:
    protected void MI_Click(object sender, DirectEventArgs e)
        { 
            
        }
    but event doesnt get called when i click a menuitem. If i add a static menu item as show below:
    <StartMenu>
                    <Items>                    
                        <ext:MenuItem ID="MenuItem1" Text="Add" runat="server">
                            <DirectEvents>
                                <Click OnEvent="MI_Click"></Click>
                            </DirectEvents>
                        </ext:MenuItem>
                    </Items>
                </StartMenu>
    then it all works fine and event gets called. My problem is that the menuitems are coming from database and i cannot add it as static items.

    Can someone please check it and tell me the reason why it is not working.
  5. #5
    Hi,

    The code with details you provided should work as expected.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Ext.Net.MenuItem mi = new Ext.Net.MenuItem()
            {
                Text = "MenuItem",
                Icon = Icon.Accept
            };
            mi.DirectEvents.Click.Event += TestHandler;
            Desktop1.StartMenu.Items.Add(mi);
        }
    
        protected void TestHandler(object sender, DirectEventArgs e)
        {
            X.Msg.Alert("TestHandler", "Hello from DirectEvent!").Show();
        }
    </script>
    
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:Desktop ID="Desktop1" runat="server" BackgroundColor="black">
            <Modules>
                <ext:DesktopModule ModuleID="DesktopModule1" WindowID="DesktopWindow1" />
            </Modules>
            <Shortcuts>
                <ext:DesktopShortcut ModuleID="DesktopModule1" Text="Shortcut" />
            </Shortcuts>
        </ext:Desktop>
        <ext:DesktopWindow ID="DesktopWindow1" runat="server" Title="DesktopWindow" />
        </form>
    </body>
    </html>
  6. #6
    Quote Originally Posted by Daniil View Post
    Hi,

    The code with details you provided should work as expected.

    Example
    Thanks Daniil for your response, but it is not working on my side as expected. Firstly i am not able to put the below alert code on menuitem click event:
    X.Msg.Alert("TestHandler", "Hello from DirectEvent!").Show();
    It says 'X' does not exist in current context. So i changed the code somehow according to my requirement.

    Since i want to add a dynamic window on the click event of menuitem, below is the code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage1.aspx.cs" Inherits="CRApps_CTS_TestPage1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Test page 1</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:resourcemanager runat="server" />
        <ext:desktop id="Desktop1" runat="server" >
            <Content>
                <asp:Label ID="label1" runat="server" Text ="No" ></asp:Label>
            </Content>
            <Modules>
                <ext:DesktopModule ModuleID="DesktopModule1" WindowID="DesktopWindow1" />
            </Modules>
            <Shortcuts>
                <ext:DesktopShortcut ModuleID="DesktopModule1" Text="Shortcut" />
            </Shortcuts>
        </ext:desktop>
        <ext:desktopwindow id="DesktopWindow1" runat="server" title="DesktopWindow" />
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Web.UI.HtmlControls;
    using cool = Ext.Net;
    using Ext.Net.Utilities;
    
    public partial class CRApps_CTS_TestPage1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cool.MenuItem mi = new Ext.Net.MenuItem()
                {
                    Text = "MenuItem",
                    Icon = cool.Icon.Accept
                };
                mi.DirectEvents.Click.Event += TestHandler;
                Desktop1.StartMenu.Items.Add(mi);
            }
        }
    
        protected void TestHandler(object sender, cool.DirectEventArgs e)
        {
            label1.Text = "Test";
    
            var win = new cool.DesktopWindow
            {
                ID = "Window1",
                Title = "My Window",
                Height = 185,
                Width = 350,
                Padding = 5,
                Html = "A new Window created at: " + DateTime.Now.ToLongTimeString(),
                AutoShow = true
            };
    
            cool.DesktopModule DesktopModule2 = new cool.DesktopModule()
            {
                ModuleID = "DesktopModule1",
                WindowID = win.ID,
                AutoRun = true
            };
    
            Desktop1.Modules.Add(DesktopModule2);
    
            cool.DesktopShortcut DesktopShortcut1 = new cool.DesktopShortcut();
            DesktopShortcut1.ModuleID = "DesktopModule1";
            DesktopShortcut1.Text = "";
    
            Desktop1.Shortcuts.Add(DesktopShortcut1);
    
            win.Render(this.form1);
        }
    }
    The TestHandler event doesnt get called when i click on a menuitem so the Desktopwindow is not getting added and not even the label control getting updated. The PageLoad event occurs and then it gives an error saying 'System.Web.HttpException: The control with ID 'ctl05' is not Observable'.

    Can you help me out please. Do i missing some dll or some reference.
    Last edited by vickygajula; Dec 10, 2010 at 10:23 AM.
  7. #7
    It says 'X' does not exist in current context. So i changed the code somehow according to my requirement.
    Please try cool.X.

    not even the label control getting updated
    To update a label during DirectEvent it needs to use .Update() method.

    Example
    label1.Text = "Test";
    label1.Update();
    The event gets called here but the Desktopwindow is not getting added
    I'm not sure this is possible to add a DesktopWindow, DescktopModule, Shortcut during DirectEvent. But I will discuss this possibility with Dev team.

    if (!IsPostBack)
    {
    ...
    }
    I guess it needs to remove the above to get MenuItem's DirectEvent working.
  8. #8
    Quote Originally Posted by Daniil View Post
    To update a label during DirectEvent it needs to use .Update() method.

    Example
    label1.Text = "Test";
    label1.Update();
    The label1.Update() didnt work for me. Below is the code for my example page:
    TestPage1.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage1.aspx.cs" Inherits="CRApps_CTS_TestPage1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Test page 1</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <div id="div1" runat="server" style="width: 1000px;">
            <ext:Desktop ID="desktop" runat="server" BackgroundColor="white" ShortcutTextColor="Black"
                Width="900px">
                <Listeners>
                    <Ready Handler="var d=#{desktop}.getDesktop(); d.tile();" />
                </Listeners>
                <Content>
                    <ext:Label ID="lblmsg" runat="server" Text="Yes">
                    </ext:Label>
                </Content>
                <Modules />
                <StartMenu>
                    <Items>
                        <ext:MenuItem ID="MenuItem1" Text="Cascade Windows" runat="server">
                            <Listeners>
                                <Click Handler="var d=#{desktop}.getDesktop(); d.cascade();" />
                            </Listeners>
                            <DirectEvents>
                                <Click>
                                </Click>
                            </DirectEvents>
                        </ext:MenuItem>
                        <ext:MenuItem ID="MenuItem2" Text="Tile Windows" runat="server">
                            <Listeners>
                                <Click Handler="var d=#{desktop}.getDesktop(); d.tile();" />
                            </Listeners>
                        </ext:MenuItem>
                        <ext:MenuItem ID="MenuItem4" Text="Add Widget" runat="server">
                            <DirectEvents>
                                <Click OnEvent="UpdateTimeStamp">
                                    <EventMask MinDelay="500" ShowMask="true" Msg="Loading Widgets..." />
                                </Click>
                            </DirectEvents>
                        </ext:MenuItem>
                    </Items>
                </StartMenu>
            </ext:Desktop>
            <div id="dvWidgets" runat="server">
            </div>
            <ext:Panel ID="pnlWidgets" runat="server">
            </ext:Panel>
        </div>
        </form>
    </body>
    </html>
    TestPage1.aspx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Web.UI.HtmlControls;
    using cool = Ext.Net;
    using Ext.Net.Utilities;
    
    public partial class CRApps_CTS_TestPage1 : System.Web.UI.Page
    {
        #region Properties
        private string CurrentDockStatesSession
        {
            get
            {
                return (string)Session["CurrentStatesMyPortal"] ?? String.Empty;
            }
            set
            {
                Session["CurrentStatesMyPortal"] = value;
            }
        }
    
        private List<string> CurrentDockStates
        {
            get
            {
                //Store the info about the added docks in the session. 
                string dockStatesSerialized = CurrentDockStatesSession;
    
                List<string> _currentDockStates = new List<string>();
                string[] stringStates = dockStatesSerialized.Split('|');
                foreach (string stringState in stringStates)
                {
                    if (stringState.Trim() != string.Empty)
                    {
                        _currentDockStates.Add(stringState);
                    }
                }
                return _currentDockStates;
            }
        }
        #endregion
    
        #region Events
        protected void Page_Init(object sender, EventArgs e)
        {
    
            try
            {
                if (IsPostBack)
                {                
                    for (int i = 0; i < CurrentDockStates.Count; i++)
                    {
                        cool.DesktopWindow win = CreateDynamicWindow(i + 1);
                        pnlWidgets.Controls.Add(win);
                    }
                }
                else
                {
                    CurrentDockStatesSession = string.Empty;
                    Session["CurrentDockStatesMyPortal"] = string.Empty;
                }
                desktop.Tile();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        
        protected void UpdateTimeStamp(object sender, cool.DirectEventArgs e)
        {
            //cool.X.Msg.Notify("The Server Time is: ", DateTime.Now.ToLongTimeString()).Show();
            cool.X.Msg.Alert("Alert", "Window Added").Show();
    
            cool.DesktopWindow win = CreateDynamicWindow(CurrentDockStates.Count + 1);
            //pnlWidgets.Controls.Add(win);
            //dvWidgets.Controls.Add(win);
            win.Render(pnlWidgets, cool.RenderMode.AddTo);
            pnlWidgets.Update();
    
            lblmsg.Text = "Updated";
            lblmsg.Update();
    
            if (CurrentDockStatesSession == "")
                CurrentDockStatesSession = (CurrentDockStates.Count + 1).ToString();
            else
                CurrentDockStatesSession = CurrentDockStatesSession + "|" + (CurrentDockStates.Count + 1).ToString();
    
            desktop.Update();
        }
    
        #endregion
    
        #region User Methodes
        private cool.DesktopWindow CreateDynamicWindow(int number)
        {
            cool.DesktopWindow win = new cool.DesktopWindow();
            win.ID = "Window" + number.ToString();
    
            win.Title = "Window" + number.ToString();
            win.Width = Unit.Pixel(300);
            win.Height = Unit.Pixel(200);
            win.AnimCollapse = true;
            win.Collapsible = true;
    
            cool.DesktopModule DesktopModule2 = new cool.DesktopModule()
            {
                ModuleID = "DesktopModule" + number.ToString(),
                WindowID = win.ID,
                AutoRun = true
            };
    
            desktop.Modules.Add(DesktopModule2);
    
            cool.DesktopShortcut desktopshortcut1 = new cool.DesktopShortcut();
            desktopshortcut1.ModuleID = "DesktopModule" + number.ToString();
            desktopshortcut1.Text = "";
    
            desktop.Shortcuts.Add(desktopshortcut1);
    
            pnlWidgets.Controls.Add(win);
            //dvWidgets.Controls.Add(win);
    
            return win;
        }
        #endregion
    }
    Above is an example page where i am adding a dynamic desktop window and updating the Label.text on menuitem click DirectEvent. But it doesnt work.
    Even if i only update the label and dont added any windows, the label doesnt get updated. And while adding the DesktopWindow and calling the desktop.Update() method i got the 'Object Reference' error as shown in attachment file.

    Please can you go through the example file code and let me know if i am doing any mistake or else we cannot call the update() event of desktop. And also the desktop.tile() method is not working on pageload or pageinit event, it occurs on any subsequent DirectEvent after the pageload.

    One more thing can you tell me why and how desktop controls .IsInUpdatePanelRefresh property is used.

    Thanks
    Vikesh
    Attached Thumbnails Click image for larger version. 

Name:	Object Reference error.GIF 
Views:	170 
Size:	33.2 KB 
ID:	2028  
  9. #9
    Well, you have a lot of different questions. Our policy is "one question/topic - one thread".

    You first question in this thread is related to this thread's topic.

    So, I suggest to continue the discussion:

    1. About adding DesktopWindow dynamically:
    http://forums.ext.net/showthread.php...n-button-click

    2. About .tile()
    http://forums.ext.net/showthread.php...esktop-Windows

    For other questions please start new forum threads.

Similar Threads

  1. Replies: 2
    Last Post: Oct 12, 2011, 7:49 AM
  2. Replies: 8
    Last Post: Jan 25, 2011, 4:21 AM
  3. [CLOSED] Direct Methods with return doesn't work in user control
    By sharif in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 13, 2010, 12:00 PM
  4. Replies: 8
    Last Post: Jun 24, 2010, 9:39 PM
  5. Replies: 3
    Last Post: May 11, 2010, 10:36 AM

Posting Permissions