protected void Page_Load(object sender, EventArgs e) { //every time the page is processed (submitted to the // web server) this method is the FIRST event // that is processed that you can easily see and // interaction with. //this is a great place to do common code that is // required on each process of the page //Example: empty out old messages //every control on your web form is a class instance //every control has an ID property //every control must have a unique name //the ID value is used to reference the control in // your code behind //since controls are instances of a class, all rules // of OOP apply MessageLabel.Text = ""; //the web page has a flag that can be checked to see // if the web page is posting back if (!Page.IsPostBack) { //if the page is not PostBack, it means that this // is the first time the page has been displayed //you can do page initialization by testing the // IsPostBack //Create a List<T> where T is a class that has // 2 columns: a value and a text display List <DDLData> DataCollection = new List <DDLData>(); DataCollection.Add(new DDLData(1, "COMP1008")); DataCollection.Add(new DDLData(3, "DMIT1508")); DataCollection.Add(new DDLData(4, "DMIT2018")); DataCollection.Add(new DDLData(2, "CPSC1517")); //sorting a List<T> // (x,y) are placeholders for 2 records at any given time in the sort // => (lamda symbol) is part of the delegate syntax, read as "do the following" // comparing x to y is ascending // comparing y to x is descendinng DataCollection.Sort((x, y) => x.displayField.CompareTo(y.displayField)); //place the data into the dropdownlist control //a) assign the data collection to the "list" control CollectionList.DataSource = DataCollection; //b) this step is required for specific "list" controls //indicate which data value is to be assigned to the Value field and the Display text field //there are different styles in assigning this information CollectionList.DataValueField = "valueField"; CollectionList.DataTextField = nameof(DDLData.displayField); //don's preference //c)bind your data to the actual control CollectionList.DataBind(); //d)OPTIONALLY you can place a prompt line on your control CollectionList.Items.Insert(0, new ListItem("select....", "0")); } }
protected void Page_Load(object sender, EventArgs e) { //this method will execute on EACH and EVERY post back //to this page. //this method will execute on the first display of this page //To determine if the page is new or postback use IsPostBack property //this method is often used as a general method to reset //fields or controls at the start of the page processing //The label MessageLabel is used to display messages to the user //Old messages should be remove from this control on each pass //How does one reference a control on the .aspx form //To reference a form control, use the control ID name //EACH control is an object. THEREFORE alter a PROPERTY value. MessageLabel.Text = ""; //Determine if this is the first display of the page // and if so, load data into the dropdownlist if (!Page.IsPostBack) { //Create an instance of List<T> for my "fake database" data DataCollection = new List <DDLClass>(); //add data to the collection, one entry at a time DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "DMIT1508")); DataCollection.Add(new DDLClass(3, "CPSC1517")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //usually lists are sorted //The List<T> has a .Sort() behaviour (method) //(x,y) represents any two entries in the data collection at any point in time // the lamda symbol => basically means "do the following" //.CompareTo() is a method that will compare to items and return the result // of comparing two items. The result is interpreted by the Sort() to // to determine if the order needs to be changed. // x vs y is ascending // y vs x is descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //place the collection into the dropdownlist //a) assign the collection to the control (ID=CollectionList) CollectionList.DataSource = DataCollection; //b)assign the value and display portions of the dropdownlist // to specify properties of the data class CollectionList.DataTextField = nameof(DDLClass.DisplayField); CollectionList.DataValueField = nameof(DDLClass.ValueField); //c)Bind the data to the collection (physical attachment) CollectionList.DataBind(); //d)You may wish to add a prompt line at the beginning of the // list of data within the dropdownlist CollectionList.Items.Insert(0, "select..."); } }
protected void Page_Load(object sender, EventArgs e) { //this method executes BEFORE any event method on EACH processing of this web page //this is a great place to do common code that is required on EACH process of the page //Example: empty out old messages Messagelabel.Text = ""; //this is an excellent place to do page initilization of controls for the first time //checking the first time for the page uses the post back flag //IsPostBack is a boolean value (true or false) if (!Page.IsPostBack) { //if the page if not PostBack, it meanss that this if the first time the page has been displayed //you can do page initalization //create a collection of instances (class objects) that will be used to load the dropdownlist //this will simulate the loading control as if the data can from a databases table //each instance would represent a record on the database dataset //to acomplish this simulation List <DDLData> DDLCollection = new List <DDLData>(); DDLCollection.Add(new DDLData(1, "COMP1008")); DDLCollection.Add(new DDLData(3, "DMIT1508")); DDLCollection.Add(new DDLData(4, "DMIT2018")); DDLCollection.Add(new DDLData(2, "CPSC1517")); //place the data into the dropdownlist control //4 steps to this process //1) assign the data collection to the contorl CollectionList.DataSource = DDLCollection; //2) in this step, you will assign the value that will be displayed to the user and the value that will be associated //and return from the control when the user picks a particular selection //in the <select> control, this data was setup using the <option> // <option value = "xxx"> display string </option> //2 styles in setting up the control values //A) a physical string of the field name CollectionList.DataValueField = "ValueID"; //B) OOP style coding CollectionList.DataTextField = nameof(DDLData.DisplayText); //3) bind your data source to your control CollectionList.DataBind(); //4) optional is to add a prompt list to your dropdownlist CollectionList.Items.Insert(0, new ListItem("select...", "0")); //sorting a List<T> //(x,y) are place holder representing any 2 records at any given time druing the sort //=> (lamda symbol) is part of the delegate syntax, I suggest that you read this symbol as "do the following" //comparing x to y is ascending //comparing y to x is descending DDLCollection.Sort((x, y) => x.DisplayText.CompareTo(y.DisplayText)); } }
protected void Page_Load(object sender, EventArgs e) { //this method is excuted automatically on each and every pass of the page (~kinda like IsPost) //this method is executed BEFORE any event method on this page //clear any old messages OutputMessage.Text = ""; //this method is an excellant place to do page initialization //you can test to post back on the page (IsPost) // by checking Page.IsPostBack property if (!Page.IsPostBack) { //the first time the page is processed //create an intance of out list<t> DataCollection = new List <DDLClass>(); //load the collection with a series of DDLCLASS instances //create the instances using the greedy constuctor DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //use the list<t> method called .Sort to sort the contents of the list // (x,y) the x and y represent two instances at any time in your collection // x.field compared to y.field (ascending) // y.field compared to x.field (decending) DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //load the datacollection to the asp control you are interested in // we are using the DropDownList //a) assign you DataCollection to the control CollectionList.DataSource = DataCollection; //b) setup any necessary properties on your ASP control that are required to work //the dropdown list will generate the html select tag code // thus we need two properties to be set // i)the value property DataValueField // ii)the display property DataTextField // the properties are set up by assigning the dataCollection field name to teh control property CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); // C) BIND your data to the control CollectionList.DataBind(); // what about prompts? // manually place a line item at the beginning of your control CollectionList.Items.Insert(0, "select..."); } }
protected void Page_Load(object sender, EventArgs e) { //this event method is executed EACH and EVERY time // this page is processed //this event method is executed BEFORE ANY EVENT method is processed //clear out all old message OutputMessage.Text = ""; //this page is an excellent place to do page initialization // of your controls. //there is a property to test for post back of your // page called Page.IsPostBack (Razor: IsPost) if (!Page.IsPostBack) { //do 1st page initialization processing //create an instance of the data collection list DataCollection = new List <DDLClass>(); //load the data collection with dummy data //normally this data would come from your database DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //to sort a List<T> use the method .Sort() //(x,y) x and y represent any two instances in your list at any time //x.field compared to y.field : ascending //y.field compared to x.field : descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //setup the dropdownlist (radiobuttonlist, checkboxlist) //a) assign your data to the control CollectionList.DataSource = DataCollection; //b) assign the data list field names to // the appropriate control properties // i) .DataValueField this is the value of the select option // ii) .DataTextField this is the display of the select option CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c) phyiscally bind the data to the control for show CollectionList.DataBind(); //what about a prompt? //one can add a prompt to the start of the BOUND control list //one will use the index 0 to position the prompt CollectionList.Items.Insert(0, "select ...."); } }
protected void Page_Load(object sender, EventArgs e) { //This method is executed automatically on EACH and EVERY pass of the page. //This method is executed BEFORE ANY EVENT method on this page. //clear any old messages OutputMessage.Text = ""; //This method is an excellent place to do page initialization. //You can test the postback of the page (Razor IsPost) by checking the Page.IsPostBack property. if (!Page.IsPostBack) { //The first time the page is proccessed. //Create an instance of the list of T. DataCollection = new List <DDLClass>(); //Load the collection with a series of DDLClass instances. Create the instances using the greedy constructor. DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //When we use db's, this is the only thing that will be replaced. //Use the List<T> method called .Sort to sort the contents of the list. //(x,y) the x and y represent any 2 instances at any time in your collection. //x.field compared to y.field (ascending) //y.field compared to x.field (descending) DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //=> is a Lambda expression, don't mix it up with >=! //=> Means for X and Y, /do the following/, then specify what you want done. //Load your data collection to the ASP control you are interested in: DropDownList //Could also be done to a radio button list dynamically. //A) Assign your data collection to the control. CollectionList.DataSource = DataCollection; //B) Setup any neccessary properties on your ASP control that are required to properly work. //The dropdownlist will generate the HTML select tag code. Thus we need 2 properties to be set. //i) Value property (DataValueField) //ii) Display property (DataTextField) //The properties are set up by assigning the data collection field name to the control property. CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //A way to do it without hardcoding the value. //C) Bind your data to the control. CollectionList.DataBind(); //What about prompts? Manually place a line item at the beginning of your control. CollectionList.Items.Insert(0, "select ..."); } }
protected void Page_Load(object sender, EventArgs e) { //This method will execute on each and every Postback to this page. //This menthod will execute on the first display of this page //To determine if the page is new or postback, use PostBack Property. //This method is often used as a general method to reset fields or controls at the start of the page processing. //E.g. The label MessageLabel is used to display messages to the user. Old messages should be removed from this control on each pass // How does one reference a control on the .aspx form? //To reference a form control, use the control ID name //EACH CONTROL IS AN OBJECT. Therefore, ALTER A PROPERTY VALUE MessageLabel.Text = ""; //Determine if this is the first display of the page, and if so, load data into the dropdownlist if (!Page.IsPostBack) { //Create an instance of List<T> for my "Fake Database Data" DataCollection = new List <DDLClass>(); //Add data to the collection, one entry at a time DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(3, "DMIT1508")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //usually lists are sorted //The List<T> has a .Sort() behavior (method) //(x,y) represent any two entries in the data collection at any point in time //The landa symbol "=>" : basically means do the following //.CompareTo() : is a method that would compare 2 items and return the result of the comparison. //The result is interpreted by the Sort() to determine if the order needs to be changed. // x vs y is ascending // y vs x is descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //Place the collection into the dropdown list //a) assign the collection to the control (CollectionList) CollectionList.DataSource = DataCollection; //b) assign the value and display portions of the dropdownlist to specific properties of the data class CollectionList.DataTextField = nameof(DDLClass.DisplayField); CollectionList.DataValueField = nameof(DDLClass.ValueField); //c) Bind the data to the collection (physical attachment) CollectionList.DataBind(); //d) you may wish to add a prompt line at the beginning of the list of data within the dropdownlist CollectionList.Items.Insert(0, "Select..."); } }
protected void Page_Load(object sender, EventArgs e) { //Page_Load executes EACH AND EVERY time there is a posting // to this page //Page_Load is executed before any submit events //this method is an excellent place to do form initialization //you can test your postings using Page.IsPostBack //IsPostBack is the same item as IsPost in our Razor forms if (!Page.IsPostBack) { //this code will be executed only on the first pass // to this page //create an instance for the static data collection DataCollection = new List <DDLClass>(); //Add instances to this collection using the DDLClass // greedy constructor DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //sorting a list<t> //use the .Sort() method //(x,y) this represents any two items in your list //compare x.Field to y.Field, ascending order //compare y.Field to x.Field, descending order DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //put the data collection into the drop down list //a) assign the collection to the controls dataSource CollectionList.DataSource = DataCollection; //b) assign the field names to the properties of the // drop down list for data association //DataValueField represents the value of the item //DataTextField represents the display of the line item CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c) bind the data to the web control CollectionList.DataBind(); //can one put a prompt on the drop down list control? // short answer: yes CollectionList.Items.Insert(0, "select..."); } }
protected void Page_Load(object sender, EventArgs e) { //if you need to reset fields on each pass (process) of the page // you can do it here //page initialization can be done here under the IsPostBack flag //the ID= attribute on your control is the name that is use in // your code to reference the control //the ID= name is unique to the form //each control is an object and behaves as an object MessageLabel.Text = ""; //the test for posting a form back to the web server is IsPostBack if (!Page.IsPostBack) { //load the DropDownList (DDL) only on the first pass of the page //In this example a locally create List<T> will act // as the data source for the DDL DataCollection = new List <DDLClass>(); //add instances to the list DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "DMIT1508")); DataCollection.Add(new DDLClass(3, "CPSC1517")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //place the data in the DDL in alphabetic order (ascending) //(x,y) represent any two rows in the collection DataCollection //Compare x.DisplayField to y.DisplayField, ascending //Compare y.DisplayField to x.DisplayField, descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //to place a collection into the DDL control we need to do 4 steps //a) assign the collection to the control's DataSource property CollectionList.DataSource = DataCollection; //b) assign field names to the properties DataTextField and DataValueField //the DataTextField contents the data seen by the user //the DataValueField is the data returned by the control on access* CollectionList.DataTextField = "DisplayField"; CollectionList.DataValueField = "ValueField"; //c) Bind the information and data to your control CollectionList.DataBind(); //d) optional, assign a prompt to your control //such that it appears before the data CollectionList.Items.Insert(0, "select...."); } }
protected void Page_Load(object sender, EventArgs e) { //page load executes each and every time there is a posting to this page //page load is executed before any submit events //this method is and exelent place to do form initialization // you can test your postings using Page.IsPostBack //page.IsPostBack is the same as IsPost in our razor if (!Page.IsPostBack) { //this code will be executed only on the first pass to this page //create an instance for the static data collection DataCollection = new List <DDLClass>(); //Add instances to this collection using the DDL gready constructor DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //Sorting a list of <T> //using the .Sort() method //(x,y) this represents any two items in your list //Compare x.Field to y.Field; acending //Compare y.field to x.field; decending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //put the data collection into the dropdown list //a) assign the collection to the controls DataSource CollectionList.DataSource = DataCollection; //b) assign the field names to the properties of the drop down list for data association //data value field represents the value of the item //data text value represents the display value of the line item CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = "DisplayField"; //CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c) bind the data to the web control //NOTE: this statment is NOT required in a windows form application CollectionList.DataBind(); //Can one put a promt on their drop down list control //yes CollectionList.Items.Insert(0, "select ..."); } }
protected void Page_Load(object sender, EventArgs e) { //this method is executed each and every time you process this webpage //this method executes before the event method //this method is an excellent place to do initial form setup //this method is an excellent place to change any control that may need to be done for the same action under all events //example - clearing old messages // a control is referenced in the code-behind by using its ID="controlname" //Control names are global to all events MessageLabel.Text = ""; //Assume you wish to do page intialization of some type but only one when the page is first loaded if (!Page.IsPostBack) { DataCollection = new List <DDLClass>(); //load collection using greedy constructor and load to the list DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT1508")); DataCollection.Add(new DDLClass(4, "DMIT2018")); //List<T> can be sorted using .Sort(Delegate) Depending on your method, the delegate will be different. //syntax for delegate // (x,y) => x.PropertyName.CompareTo(y.PropertyName) DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //attach the data to the dropdownlist control //attach data CollectionList.DataSource = DataCollection; //assign the display text property to the ddl control - there are a couple of ways //way THE FIRST CollectionList.DataTextField = "DisplayField"; //WAY THE SECOND //assign the value data property to the ddl control CollectionList.DataValueField = nameof(DDLClass.ValueField); //then do the databind thing CollectionList.DataBind(); //optionally add a prompt line CollectionList.Items.Insert(0, "Select ..."); } }
protected void Page_Load(object sender, EventArgs e) { //Page_Load executes each and every time there is a posting // to this page. //Page_Load is executed before any submit events //this method is an excellent place to do form initilizition //You can Test your postings using Page.IsPostBack //IsPostBack is the same item as IsPost in Razor Forms if (!Page.IsPostBack) { //this code will be executed only on the first pass // to this page //create an instance for the static data collection DataCollection = new List <DDLClass>(); //add instances to this collection using the DDLClass // greedy constructor DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //sorting a List<T> //use the .Sort() method //(x,y) this represents any two items in your list //compare x.Field to y.Field; ascending //Compare y.Field to x.Field; descending //=> lambda symbol. Think of it as Do the Following DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //Put the data collection into the dropdownlist //a) assign the collection to the controls DataSource CollectionList.DataSource = DataCollection; //b) assign the field names to the properties of the // dropdownlist for data association //DataValueField represents the value of the item //DataTextField represents the display of the line item CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c) bind the data to the web control //NOTE: This Statement is NOT required in a Windows Form Application CollectionList.DataBind(); //Can one put a prompt on their dropdownlist control? // Obvs, Yes. CollectionList.Items.Insert(0, "Select..."); } }
protected void Page_Load(object sender, EventArgs e) { // load empty feilds on first page refresh Message.Text = ""; //------------------DROP DOWN LIST------------------------ //the web page has a flag that can be checked to see // if the web page is posting back if (!Page.IsPostBack) { //if the page is not PostBack, it means that this // is the first time the page has been displayed //you can do page initialization by testing the // IsPostBack // LIST is a bunch of the same classes //Create a List<DDLdata> where T is a class that has // 2 columns: a value and a text display List <DDLData> ReviewStars = new List <DDLData>(); // adding values to the list ReviewStars.Add(new DDLData(1, "1 star")); ReviewStars.Add(new DDLData(2, "2 stars")); ReviewStars.Add(new DDLData(3, "3 stars")); ReviewStars.Add(new DDLData(4, "4 stars")); ReviewStars.Add(new DDLData(5, "5 stars")); // SORT ReviewStars.Sort((x, y) => x.displayField.CompareTo(y.displayField)); // grab form id and place Review list into drop down list CollectionList.DataSource = ReviewStars; // if there is a value and text feild do this CollectionList.DataValueField = "valueField"; CollectionList.DataTextField = nameof(DDLData.displayField); //bind your data CollectionList.DataBind(); // add in an insert CollectionList.Items.Insert(0, new ListItem("select...", "0")); } }
protected void Page_Load(object sender, EventArgs e) { OutputMessage.Text = ""; //this event method is executed EACH and EVERY time this page is processed //this event method is executed BEFORE ANY EVENT method is processed //this page is an excellent place to do page initialization of your controls //there is a property to test for post back of your page called Page.IsPostBack (Razor: IsPost) if (!Page.IsPostBack) { //do 1st page initialization processing //create an intence of the Data colletion list DataCollection = new List <DDLClass>(); //load the data collection with dummy data, normally this data would com from your database DataCollection.Add(new DDLClass(1, "Comp1008")); DataCollection.Add(new DDLClass(2, "Cpsc1517")); DataCollection.Add(new DDLClass(3, "Dmit2018")); DataCollection.Add(new DDLClass(4, "Dmit1508")); //to sort List<T> use the method .Sort() //(x,y) x and y represent any two instances in your list at any time. // x.firld compared to y.field : ascending // y.field compared to x.field : desending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //setup the dropdown list //a, assign your data to the control CollectionList.DataSource = DataCollection; //b, assign the data list field names to the approriate control properties // i), .DatavalueField this is the value of the selection // ii) .DataTextFiled this is the display of the selection optio CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //bind the data to the control for show CollectionList.DataBind(); //promt //one can adda prompt to the start of the BOUND controllist //one will use the index 0 to position the prompt CollectionList.Items.Insert(0, "plese select"); } }
protected void Page_Load(object sender, EventArgs e) { //Page_Load executes EACH AND EVERY TIME there is a posting to this page //Page_Load is executed BEFORE any submit events //this method is an excellent place to do form initialization. you can test your postings using Page.IsPostBack //IsPostBack == IsPost() in Razor forms //only executed on first pass, not ispost //create an instance for the static data collection if (!Page.IsPostBack) { DataCollection = new List <DDLClass> { //Add instances to this collection using the DDLClass greedy constructor new DDLClass(1, "COMP1008"), new DDLClass(2, "CPSC1517"), new DDLClass(3, "DMIT2018"), new DDLClass(4, "DMIT1508") }; //sorting a List<T> //use the .Sort() method //(x,y) this represents any 2 items in your list //compare x.Field to y.Field; ascending //compare y.Field to x.Field; descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //put the data collection into the dropdownlist (DDL) //A) assign the collection to the controls DataSource CollectionList.DataSource = DataCollection; //B) assign the field names to the properties of the DDL for data association //DataValueField represents the value of the item //DataTextField represents the display of the line item CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = "DisplayField"; //OR CollectionList.DataTextField = nameof(DDLCLass.DisplayField); //C) bind the data to the web control //NOTE: this statement is NOT required in a Windows Form Application CollectionList.DataBind(); //you can put a prompt on their dropdownlist control CollectionList.Items.Insert(0, "Select ..."); }//closes !Page.IsPostBack }
protected void Page_Load(object sender, EventArgs e) { //Page_Load executes each and every time there is a posting to this page //it is executed before any submit events //This method is an exelent place to do form initialization //you can test your postings using Page.IsPostBack //IsPostBack is the same item as IsPost from the Razor forms if (!Page.IsPostBack) { //create an instance for the static data collection DataCollection = new List <DDLClass>(); //Add instance to this collection using the DDLClass greedy constructor DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //sorting a List<T> //use the .sort()method //(x,y) represents any 2 items in the list //compare x.field to y.field; ascending //if we compare y.field to x.field; descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //put the data Collection into the drop down list //a)assign the collection to the controls data source CollectionList.DataSource = DataCollection; //b) assign the field names through the properties of the drop down list for data association //DateValueField rpresents the value fot he item //DataTextField represents the display of the line item CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c)bind the data to the web control //NOTE: This statement is not required in a windows form app CollectionList.DataBind(); //can one put a prompt on their drowdownlist control //yes CollectionList.Items.Insert(0, "select ..."); } }
//this method is executed every time the page is processed //this event method is executed before any event method is processed //this is a good place to do initialization of your controls //there is a property to test for postback of your page protected void Page_Load(object sender, EventArgs e) { //clear old messages OutputMessage.Text = ""; if (!Page.IsPostBack) { //page init DataCollection = new List <DDLClass>(); //load the data collection with dummy data //normally this data would come from your database DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "CPSC1517")); DataCollection.Add(new DDLClass(3, "DMIT2018")); DataCollection.Add(new DDLClass(4, "DMIT1508")); //sort arraylist //(x,y) //x.field compared to y.field = ascending //y.field compared to x.field = decending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); //setup the dropdown list (works for radiobutton or dropdown lists as well) //a) assign your data to the control CollectionList.DataSource = DataCollection; //b) assign the data list field names to the appropriate control properties // i) .DataValueField this is the value of the select option // ii) .DataTextField this is the display of the select option CollectionList.DataValueField = "ValueField"; CollectionList.DataTextField = nameof(DDLClass.DisplayField); //c) physically bind the data to the control for show CollectionList.DataBind(); //what about a prompt? //one can add a prompt to the start of the BOUND control list //one will use the index 0 to position the prompt CollectionList.Items.Insert(0, "select..."); } }
protected void Page_Load(object sender, EventArgs e) { //Every time the page is processed (submitted to the web server) this method is the FIRST event that is processed that you can easily see and interact with //This is a great place to do common code that is required on each process of the page //Example: empty out old messages //Every Control on your web form is a class instance //Every control has an ID property //Every control must have a unique name //The ID value is used to reference the control in your code behind //Since controls are instances of a class, all rules of OOP apply MessageLabel.Text = ""; //is the ID of the field that is shown on the bottom of the page (our output field) //The web page has a flag that can be checked to see if the web page is posting back if (!Page.IsPostBack) { //If the page is NOT PostBack, it means that this is the first time the page has been displayed //You can do page initialization by testing the IsPostBack //Create a List<T> where T is a class that has 2 columns: a value and a text display //In die Klammer kommt die Type of objects (wo jetzt das T steht --> zb int oder float) //Das T ist der Type of object, in diesem Fall ist der Type of object eine class die wir programmmiert haben ist oder build in ist //int x //List DataCollection List <DDLData> DataCollection = new List <DDLData>(); //DataCollection is the name of the property in the Class Page called "DDL Data DataCollection.Add(new DDLData(1, "COMP1008")); // new DDLData --> ist ein Object createn DataCollection.Add(new DDLData(3, "DMIT1508")); DataCollection.Add(new DDLData(4, "DMIT2018")); DataCollection.Add(new DDLData(2, "CPSC1517")); //Wir brauchen hierfuer einen Constructor weil wenn wir eine instance von ner object createn dann ist die Zahl und der Name required //Wenn wir nix eingeben wuerden wuerde der leere constructor geurfen (nix in der Klammer) //Sorting a List<T> //(x,y) are placeholders for 2 records at any given time in the sort // => (lamda symbol) is part of the delegate syntax, read as "do the following" //Comparing x to y is ascending //Comparing y to x is descending DataCollection.Sort((x, y) => x.displayField.CompareTo(y.displayField)); //Place the data into the dropdownlist control //a) assign the data collection to the "list" Control CollectionList.DataSource = DataCollection; //CollectionList is the ID of the DROPDOWN and DataCollection of the List we created //b) This step is required for specific "list" controls //Indicate which data value is to be assigned to the Value field and the Display text field //There are different styles in assigning this information CollectionList.DataValueField = "valueField"; CollectionList.DataTextField = nameof(DDLData.displayField); //don's preference //Other controls that handle lists we don't need B; we only need A and C //c)Bind your data to the actual control CollectionList.DataBind(); //CollectionList is the name of the Dropdown //d)OPTIONALLY you can place a prompt line on your control CollectionList.Items.Insert(0, new ListItem("Select...", "0")); //Die erste 0 ist die Stelle des Indexes (in diesem Falle an "1." Stelle) //("Select...", "0") --> is a String and the Value (NOTE: we could also just put in the "Select..." without the Value) } }
protected void Page_Load(object sender, EventArgs e) { //this method executes BEFORE any event method on // EACH processing of this web page //this is a great place to do common code that is // required on EACH process of the page //Example: empty out old messages MessageLabel.Text = ""; //this is an excellent place to do page initialization // of controls on the 1st pass //checking the 1st time for this page uses the post back flag //Page.IsPostBack; this is a boolean value(true or false) if (!Page.IsPostBack) //if(!false) => true { //if the page is not PostBack, it means that this // is the 1st time the page has been displayed //simulate the loading of the dropdownlist via a // dataset from a database call //create a collection of instances (class objects) // that will be used to load the dropdownlist //each instance will represent a record on the // database dataset //to accomplish is simulation, we will create a // class and use it with a List<T> //the <T> in this example is the class DDLData List <DDLData> DDLCollection = new List <DDLData>(); DDLData aninstance = new DDLData(); aninstance.ValueId = 1; aninstance.DisplayText = "COMP1008"; DDLCollection.Add(aninstance); aninstance = new DDLData(3, "DMIT1508"); DDLCollection.Add(aninstance); DDLCollection.Add(new DDLData(4, "DMIT2018")); DDLCollection.Add(new DDLData(2, "CPSC1517")); //sorting of a List<T // (x,y) are placeholders representing any 2 records at any // given time during the sort // => (lamda symbol) is part of the delegate syntax, I suggest // that you read this symbol as "do the following" // comparing x to y is ascending // comparing y to x is descending DDLCollection.Sort((x, y) => x.DisplayText.CompareTo(y.DisplayText)); //place the data collection set into the dropdownlist control //3 step to this process //a) assign the data collection set to the control CollectionList.DataSource = DDLCollection; //b) in this step you will assign the value that will // be diplayed to the user and the value that will // be associated and return from the control wher // the user picks a particular selection //in the <select> control, this data was step using the <option> // <option value="xxxx">display string</option> //this information is assigned to the DDL control by use // of the property name in your collection //2 styles // a) physical string of the field name CollectionList.DataValueField = "ValueId"; // b) OOP style coding CollectionList.DataTextField = nameof(DDLData.DisplayText); //c) bind your data source to your control CollectionList.DataBind(); //d) optionally // add a prompt line to your dropdownlist CollectionList.Items.Insert(0, new ListItem("select ...", "0")); } }
protected void Page_Load(object sender, EventArgs e) { // this method will execute on EACH and EVERY post back to this page // this method will execute on the first display of this page // to determine if the page is new or postback, use the IsPostBack property // this method is often used as a general method to reset fields or controls at the start of page processing // the label MessageLabel is used to display messages to the user. Old messages should be removed from this control on each pass // how do we reference a control on the .aspx form? notice that each form control has an ID, we can use this. // each control is an object, therefore we will be altering property values MessageLabel.Text = ""; if (!Page.IsPostBack) // "!" means NOT { // here we are creating an instance of List<T> for the "fake database" data DataCollection = new List <DDLClass>(); // add data to the collection that we just created, one entry at a time DataCollection.Add(new DDLClass(1, "COMP1008")); DataCollection.Add(new DDLClass(2, "DMIT1508")); DataCollection.Add(new DDLClass(3, "CPSC1517")); DataCollection.Add(new DDLClass(4, "DMIT2018")); // one benefit of using a list vs an array is that we can easily sort them, we do this below using the .Sort() method // (x, y) represents ANY two entries in the data collection at ANY point in time // the lamda symbol "=>" basically means "do the following" // .CompareTo() is a mehtod that will compare two items and return the result // this result is then interpreted by the Sort() method to determind if the order needs to be changed // x vs y is ascending // y vs x is descending DataCollection.Sort((x, y) => x.DisplayField.CompareTo(y.DisplayField)); // place the collection into the dropdownlist // a) assign the collection to the control (ID = CollectionList CollectionList.DataSource = DataCollection; // b) assign the value and display portions of the dropdownlist to specify properties of the data class CollectionList.DataTextField = nameof(DDLClass.DisplayField); CollectionList.DataValueField = nameof(DDLClass.ValueField); // c) bind the data to the collection (aka physical attachment) CollectionList.DataBind(); // d) you may wish to add a prompt line at the beginning of the list of data within the dropdown list CollectionList.Items.Insert(0, "select..."); } } // closes PageLoad