create direct event in runtime to MenuItem

  1. #1

    create direct event in runtime to MenuItem

    Dear All,
    I Need your Help
    how can i add direct event to MenuItem in Runtime , this is my code :

    asp.net Designer

    <ext:MenuItem  ID="MenuItem1" runat="server" Width="300" Text="Select Period" Icon="BookOpen">
                        <Menu>
                            <ext:Menu  ID="Menu2" runat="server" BoxMinWidth="300">
                                <Items/>
                                <DirectEvents>
                                <ItemClick OnEvent="SetPeriodSession" />
                                </DirectEvents>
                            </ext:Menu>
                        </Menu>
                    </ext:MenuItem>
    Code:
    public void FillPeriods()
        {
            SqlConnection con = Connections.GetSQLConnection();
            
            string command="SELECT dbo.Schedules.EffectiveDate, dbo.Schedules.ScheduleCode, dbo.Schedules.GroupID,"
            +"dbo.Schedules.PeriodID FROM dbo.Schedules INNER JOIN dbo.Teachers ON dbo.Schedules.TeacherID ="
            +"dbo.Teachers.TID WHERE (dbo.Schedules.TeacherID = @TID)ORDER BY dbo.Schedules.PeriodID";
    
    
            SqlCommand com=new SqlCommand(command,con);
            com.Parameters.AddWithValue("@TID", Session["TeacherID"].ToString());
            con.Open();
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                Ext.Net.MenuItem.Config config = new Ext.Net.MenuItem.Config();
                config.Icon = Icon.BookEdit;
                config.Text = reader[3].ToString();
                config.DirectEvents.Click.ExtraParams.Add(new Ext.Net.Parameter("selected", reader[3].ToString()));
                config.DirectEvents.Click.Event += SetPeriodSession;
                
                
                Menu2.Items.Add(new Ext.Net.MenuItem(config));
            }
            Menu2.DataBind();
            reader.Close();
            con.Close();
        }
    
    
        
        public void SetPeriodSession(object o , DirectEventArgs e)
        {
            if (e.Success)
            {
                Session["PeriodNO"] = e.ExtraParams[0].Value;
                SqlConnection con = Connections.GetSQLConnection();
                SqlCommand com = new SqlCommand("select GroupID from Schedules where PeriodID=@PID and TeacherID=@TID and status='Active'", con);
                com.Parameters.AddWithValue("@PID", Session["PeriodNO"].ToString());
                com.Parameters.AddWithValue("@TID", Session["TeacherID"].ToString());
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    Session["GroupNO"] = GroupsOperations.GetGroupNo(int.Parse(dr[0].ToString()));
                    lblGroupNo.Text = Session["GroupNO"].ToString();
                }
                dr.Close();
                con.Close();
            }
        }
    ----------------------------------------------------------------------------------------------------------------------------------
    Now I need to create direct event in runtime to execute code in Method (SetPeriodSession)
    i used the previous code , but it's not working ???

    thanks in advance
    Last edited by geoffrey.mcgill; Jan 08, 2012 at 6:04 PM. Reason: please use [CODE] tags
  2. #2
    Hi,

    I'm not 100% sure where you call the FillPeriods method, but guess during another DirectEvent.

    If yes, please see:
    http://forums.ext.net/showthread.php...ll=1#post60155
  3. #3
    I tried More events but it's still not working :(
  4. #4
    Please clarify what do you mean under "More events"?
  5. #5
    i used (Click & ItemClick) , and tried to work with handler with script :
    
    <script type="text/javascript">
             var getSelectedItem = function (mp) {
                 var index = parseInt(mp.getSelIndexField().getValue()),
                    item;
                 if (!isNaN(index)) {
                     item =menu2.items.get(index);
                 }
                 alert(item ? item.text : "No selected item");
             };
        </script>

    but it's still not working.
  6. #6
    Please demonstrate how you use Click and ItemClick events.
  7. #7

    Done

    Thanks My GOD , I found the Solution :

    In Designer no need to create Directevent
     <ext:Menu  ID="Menu2" runat="server" BoxMinWidth="300">
                                <Items/>
                                <%--<DirectEvents>
                                <ItemClick OnEvent="SetPeriodSession" />
                                </DirectEvents>--%>
                            </ext:Menu>
    in C# Code i Canceled the Config and Create Ext.Net.MenuItem Directly with a Click Event having a parameter .

    
        public void FillPeriods()
        {
            SqlConnection con = Connections.GetSQLConnection();
            
            string command="SELECT dbo.Schedules.EffectiveDate, dbo.Schedules.ScheduleCode, dbo.Schedules.GroupID,"
            +"dbo.Schedules.PeriodID FROM dbo.Schedules INNER JOIN dbo.Teachers ON dbo.Schedules.TeacherID ="
            +"dbo.Teachers.TID WHERE (dbo.Schedules.TeacherID = @TID)ORDER BY dbo.Schedules.PeriodID";
    
    
            SqlCommand com=new SqlCommand(command,con);
            com.Parameters.AddWithValue("@TID", Session["TeacherID"].ToString());
            con.Open();
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                Ext.Net.MenuItem mi = new Ext.Net.MenuItem();
                mi.Icon = Icon.BookAdd;
                mi.Text = reader[3].ToString();
                mi.DirectEvents.Click.ExtraParams["PID"]= reader[3].ToString();
                mi.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(SetPeriodSession);
    
    
                Menu2.Items.Add(mi);
            }
            Menu2.DataBind();
            reader.Close();
            con.Close();
        }
    
    
      
        public void SetPeriodSession(object o, DirectEventArgs e)
        {
            Session["PeriodNO"] = e.ExtraParams[0].Value;
                SqlConnection con = Connections.GetSQLConnection();
                SqlCommand com = new SqlCommand("select GroupID from Schedules where PeriodID=@PID and TeacherID=@TID and status='Active'", con);
                com.Parameters.AddWithValue("@PID", Session["PeriodNO"].ToString());
                com.Parameters.AddWithValue("@TID", Session["TeacherID"].ToString());
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    Session["GroupNO"] = GroupsOperations.GetGroupNo(int.Parse(dr[0].ToString()));
                    lblGroupNo.Text = Session["GroupNO"].ToString();
                }
                dr.Close();
                con.Close();
    
    
                ShowAttendance();
        }
    Now It's Work

    thank you Daniil
    you can close thread Now.
    Last edited by elbanna23; Jan 10, 2012 at 12:55 PM.
  8. #8
    Thanks for sharing the solution, it can help someone on the forums in the future.

Similar Threads

  1. Replies: 1
    Last Post: May 09, 2012, 7:08 PM
  2. create event for gridpanel at runtime
    By kyawthura.mr in forum 1.x Help
    Replies: 4
    Last Post: Aug 10, 2011, 9:20 AM
  3. [CLOSED] Confirm to MenuItem with direct method.
    By stoque in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 01, 2011, 7:56 PM
  4. Replies: 3
    Last Post: Apr 20, 2010, 12:21 PM
  5. Replies: 1
    Last Post: Dec 03, 2009, 10:57 AM

Posting Permissions