public static List <DDLClass> DataCollection; //if you take out static, everyone has their own version of the page. with static, one copy for everyone protected void Page_Load(object sender, EventArgs e) { //this event will happen EACH AND EVERY time your page is executed //this event will happen BEFORE ANY of YOUR control events happen //this is a great place to initilize items(controls) that are common //to many events and need to be done every time //e.x.: old messages should be cleared out on every pass // you can empty the MessageLabel control under EACH event // OR you can do it ONCE here for ALL events MessageLabel.Text = ""; //this is a great place to do 1st time initialization of controls // (similar to the else side of the IsPost from Razor) if (!Page.IsPostBack) //same thing as (Page.IsPostBack == false) or (Page.IsPostBack != true) { //load the DDL on the first pass. //create an instance of a collection (List<T>; T == class DDLClass) //create 4 record instances for the collection //place the collection into the DDL DataCollection = new List <DDLClass>(); DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT1508")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //sort the data collection for the ddl by program course name //syntax: collectionname.Sort((x,y) => x.fieldname.CompareTo(y.fieldname)) //collectionname is where your data resides (List<t>) //(x,y) represent any two values (records) in your collection at any point in time //=> (lamda) can be thought of as "do the following to x and y" (delegate) //our delegate for lamda is comparing x and y on the fieldname // x CompareTo y is an ascending sort // y CompareTo x is a descending sort DataCollection.Sort((y, x) => x.DisplayField.CompareTo(y.DisplayField)); //steps in loading your DDL //assume that the DataCollection is actually database data // a) assign the data source to the control CollectionChoiceList.DataSource = DataCollection; //collectionchoicelist is id of what was used for the dropdownlist in BasicControls.aspx //<option value=somevalue> sometext </option> //define what somevalue and sometext is in the data collection // b) indicate the display field to the control CollectionChoiceList.DataTextField = "DisplayField"; //Non-Object Style // c) indicate the value field to the control CollectionChoiceList.DataValueField = nameof(DDLClass.ValueField); //Object Style; preferable because you can use intellisense // d) physically bind the data to the control CollectionChoiceList.DataBind(); //optional prompt line?? CollectionChoiceList.Items.Insert(0, "select ..."); //Index for COMP1008 changes from 3 to 4? } }
protected void Page_Load(object sender, EventArgs e) { //this event will happen EACH and EVERY time your page is executed //this event will happen BEFORE ANY of YOUR control events happen //this is a great place to initialize items (controls) that are // common to many events and need to be done every time //example: old messages should be cleared out on every pass // you can empty the MessageLabel control under each event // OR do it once here all ALL events MessageLabel.Text = ""; //this is a great place to do 1st time initialization of controls //(similar to the else side of the IsPost from Razor) if (!Page.IsPostBack) //(Page.IsPostBack == false) { //load the DDL on the 1st pass //create an instance of a collection (List<T>) (T == class called DDLCLass) //create 4 instance of DDLClass for the collection //place the collection into the DDL DataCollection = new List <DDLClass>(); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(4, "DMIT2018")); DataCollection.Add(new DDLClass(3, "DMIT1508")); //sort the data collection for the ddl by program course name //syntax collectionname.Sort((x,y) => x.fieldname.CompareTo(y.fieldname)) //collectionname is where your List<T> resides //(x,y) represents any two values (records) in your collection at any point in time //=> (lamda) can be thought of as "do the following to x an y" (delegate) //our delegate for lamba is comparing x and y on the fieldname // x CompareTo y is an ascending sort // y CompareTo x is a descending sort DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //steps in loading your DDL //assume that the DataCollection is actually database data //a) assign the data source to the control CollectionChoiceList.DataSource = DataCollection; //just the data //<option value=theValue> theDisplay </option> //b) indicated the display field to the controller CollectionChoiceList.DataTextField = "DisplayField"; //non object style //c) inidcated the value field to the controller CollectionChoiceList.DataValueField = nameof(DDLClass.ValueField); //object style //d) physically bind the data to the control CollectionChoiceList.DataBind(); //optional prompt line?? CollectionChoiceList.Items.Insert(0, "select ...."); } }
protected void Page_Load(object sender, EventArgs e) { //this event will happen EACH and EVERY time your page is executed //this event will happen BEFORE ANY of YOUR control events happen //this is a great place to intialize items (controls) that are common // to many events and need to be done every time //example: old messages should be cleared out on every pass // you can empty the MessageLabel control under each event // OR do it once here for ALL events MessageLabel.Text = ""; //this is a great place to do 1st time intialization of controls //(similar to the else side of the IsPost from Razor) if (!Page.IsPostBack) //Page.IsPostBack == false { //load the DDL on the first pass //create a new instance of the DDLClass //create 4 entries for the collection //place the collection into the DDL DataCollection = new List <DDLClass>(); DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT1508")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //SORT the data collection for the DDL by course name //syntax: CollectionName.Sort((x,y) => x.fieldname.CompareTo(y.namefield)) //collection name is where your List<t> resides //(x,y) represent and TWO values (records) in your collection at any point in time // =>(lamda) can be thought of as "do the following to x and y" // our delgate for => is comparing x and y on fieldname //x compare to y is an ascending sort //y compare to x is an descending sort DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //steps in loading your DDL //A) Assign the datasource to the control CollectionChoiceList.DataSource = DataCollection; //B) Indicate the display field to the control //<option value=valuefield>DisplayField</option> CollectionChoiceList.DataTextField = "DisplayField"; //nonobject style //C) Indicate the value field to the control CollectionChoiceList.DataValueField = nameof(DDLClass.ValueField); //object style //D) Physically bind the data to the control CollectionChoiceList.DataBind(); //Optional promptline CollectionChoiceList.Items.Insert(0, "select..."); } }
protected void Page_Load(object sender, EventArgs e) { //This event will happen EACH and EVERY time your page is executed //this event will happen BEFORE ANY of YOUR control events happen //this is a great place to initialise items(controls) that are common // to many events. and need to be done everytime //Example: // old messages should be cleared our on every pass // you can empty the MessageLabel control under each event // OR DO it ONCE here for ALL events MessageLabel.Text = ""; //this is a great place to do first time initialization of controls. // (simlilar to the else side of the "IsPost" from Razor) //----- if (!Page.IsPostBack) //or if(Page.IsPostBack == false)or if(Page.IsPostBack != true) { //load the DDL on the 1st pass //create an instane of a collection (List<T>); T = class DDLClass) //create 4 record instances for the collection //place the collection into the DDL //normaly, the collection is the result of a qury to your database } DataCollection = new List <DDLClass>(); DataCollection.Add(new DDLClass(1, "COMP 1008")); DataCollection.Add(new DDLClass(2, "CPSC-1517")); DataCollection.Add(new DDLClass(3, "DMIT-1508")); DataCollection.Add(new DDLClass(4, "DMIT-2018")); //sort the data collection for the ddl by program course name //Syntax: collectionname.Sort((x,y) => x.fieldname.Compare(y.fieldname)) //collectionname is where your data resides (List<T>). //(x,y) represents any two values (records0 in your collection in any point in time // = (lamda) can be though of as "do the following to x and y" (delegate) //our delegate for lamda is compairing x and y on the fieldname // x CompaireTo y is an ascending sort // y CompairTo x is an descending sort DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //steps in loading your DDL: //assume the DataCollection is actually database data //a) assign the data source to the controll. CollectionChoiceList.DataSource = DataCollection; //just the data //<option value ="somevalue"> some text </option> //defince what somevalue and sometext is in the data collection //b.) indicate the dicplay field to the control CollectionChoiceList.DataTextField = nameof(DDLClass.DisplayField); //non-object style //c.) indicate the value field to the control CollectionChoiceList.DataValueField = nameof(DDLClass.ValueField); //this is perferable since that the program is //ensured that the correct object is called //d.)physically bind the data to the control CollectionChoiceList.DataBind(); //optional prompt line?? CollectionChoiceList.Items.Insert(0, "select..."); }