Saturday 19 November 2016

asp.net webform pass parameter by session




//enroll.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 Enrollment : System.Web.UI.Page
    {
        List<int> courses = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            //get the id collection from session and if it doesn't exist we add it
            if (Session["courses"] != null)
                courses = (List<int>)Session["courses"];
            else
                courses = new List<int>();
        }

        protected void uxEnrol_Click(object sender, EventArgs e)
        {
            //get the course id from the drop down and add the value to the list
            var id = int.Parse(uxCourses.SelectedValue);
            courses.Add(id);

            //add the list to the session
            Session["courses"] = courses;
            //or
            //Session.Add("courses", courses);
        }
    }
}

-----------------------------------------------------------
//payment.cs

using Ctti.Data;
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 Payment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //get the courses id collection from session if it exists
                if(Session["courses"] != null)
                {
                    var courses = (List<int>)Session["courses"];
                    var db = new CTTIEntities1();
                    var list = db.Courses.Where(c => courses.Contains(c.Id)).ToList();
                    uxCourses.DataSource = list;
                    uxCourses.DataBind();
                    //uxCourses.Columns[0].Visible = false;
                }
            }
        }


        protected void uxCourses_PreRender(object sender, EventArgs e)
        {
            if (uxCourses.Columns.Count > 0)
                uxCourses.Columns[0].Visible = false;
        }

        //This is the event to capture when each row is being added and...
        protected void uxCourses_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //...make the first cell of the row being added invisible!!!
            e.Row.Cells[0].Visible = false;
        }
    }
}

No comments:

Post a Comment