Tuesday 22 November 2016

.asp webform user control event


add control folder, add cousescontrol.ascx

add drop down list to cousescontrol.ascx
change auto post back to true in property window

//course control.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ctti.Data;

namespace CttiDemo.Controls
{
    public partial class CoursesControl : System.Web.UI.UserControl
    {
        //Step 3: define the public event
        public event CourseSelectionDelegate CourseSelect;

        private CTTIEntities1 DB = new CTTIEntities1();

        //Property returns the selected value of the DDL as an int instead of string
        public int SelectedId
        {
            get
            {
                return int.Parse(uxCourses.SelectedValue);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //populate the DDL
            if (!IsPostBack)
            {
                uxCourses.DataSource = DB.Courses.Select(c => new { Id = c.Id,
                                                         Name = c.CourseName }).ToList();
                uxCourses.DataTextField = "Name";
                uxCourses.DataValueField = "Id";
                uxCourses.DataBind();

                //raise the event first time page loads so event handler can load form
                uxCourses_SelectionChanged(this, null);
            }
        }

        protected void uxCourses_SelectionChanged(object sender, EventArgs args)
        {
            //Step 4: raise the event
            //if event is not raised, raise event
            if(CourseSelect != null)
            {
                //we need to get the course from the data layer using the id value from DDL
                int id = int.Parse(uxCourses.SelectedValue);

                Course crs = DB.Courses.Find(id);

                var c = new CourseEventArgs
                {
                    Id = crs.Id,
                    CourseCode = crs.CourseCode,
                    CourseName = crs.CourseName,
                    Description = crs.Description,
                    Duration = crs.Duration,
                    Fee = crs.Fee
                };

                //fire event everywhere 
                CourseSelect.Invoke(this, c);
            }
        }
    }

    //Step 1: define a custom event args class
    public class CourseEventArgs : EventArgs
    {
        public int Id { get; set; }
        public string CourseCode { get; set; }
        public string CourseName { get; set; }
        public decimal Fee { get; set; }
        public int Duration { get; set; }  
        public string Description { get; set; } 
    }

    //Step 2: define a delegate
    public delegate void CourseSelectionDelegate(object sender, CourseEventArgs args);

}

------------------------------------------------------------------

drag coursecontrol onto courses.aspx webform

//course.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CttiDemo
{
    public partial class Courses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           //drop down list from coursecontrol.ascx fires event, courses.asp receives event
            uxCourses.CourseSelect += UxCourses_CourseSelect;            
        }

        //event handler
        private void UxCourses_CourseSelect(object sender, Controls.CourseEventArgs args)
        {
            uxName.Text = args.CourseName;
            uxCode.Text = args.CourseCode;
            uxFee.Text = args.Fee.ToString("c");
            uxDuration.Text = args.Duration.ToString() + " hrs";
            uxDescription.Text = args.Description;
        }
    }
}



No comments:

Post a Comment