170 likes | 441 Views
Summary. Reviewing work from week one. Summary. We will review the work done this week. Lecture One introduced the Editor and CSS Lecture Two showed how to create controls Lecture Three was about tables and css. Lecture Four concerned data. Lecture Five creates a test. VS Editor.
E N D
Summary Reviewing work from week one.
Lecture 5: Review Summary • We will review the work done this week. • Lecture One introduced the Editor and CSS • Lecture Two showed how to create controls • Lecture Three was about tables and css. • Lecture Four concerned data. • Lecture Five creates a test.
Lecture 5: Review VS Editor • The Visual Studio Editor allow you to design a web page. • The elements used include: • Panel
Lecture 5: Review Generate • You can use C# to create controls. • You can change the properties of the controls. • You must add the new control to the page. • Some controls may be added to Controls • Some controls must be added to form1.Controls
Lecture 5: Review Styling • You can change the style of controls as you create them. • Usually it is better to set CSS styles and rely on an external style sheet. • This means that the “look and feel” design work may be done after the coding is complete.
Lecture 5: Review Events • Event only take place at the server. • You can capture OnClick events – at this time you can interrogate the value of various controls. • The value of the control is usually a string – you have to convert the string into whatever value you need.
Lecture 5: Review Data - Lists • You can store data in an Array or in a SortedList. • An Array is simple and quick to create: String [] a = new String[] {"one", "two"}; • A SortedList is more powerful, you can access items using Strings and the key. • There are other structures – but SortedList is one of the best.
Lecture 5: Review Finding Controls • Sometimes you need to run over all of the controls on the page. • You may need to look in the Controls structure • You may need to look into the form1.Controls structure. • Usually only some of the controls in the lists are the ones you want.
Lecture 5: Review Testing the Type • You can use if (c is Panel) to test if the controls c is a Panel: for (inti = 0; i < form1.Controls.Count; i++) { if (form1.Controls[i] is Panel) { Panel p = (Panel)form1.Controls[i]; foreach (Control c in p.Controls) if (c is RadioButtonList) { RadioButtonListrbl = (RadioButtonList)c; if (rbl.Text == "1") score++; } } }
Lecture 5: Review Multiple Choice • You can create a multiple choice quiz. • The questions and responses are coded as data. • The system can calculate the user’s score. • You can use a Class to represent a single question.
Lecture 5: Review Question Class public class Question { public String questionText; public String[] distractors; public int correct; public Question(String questionText, String[] distractors, int correct) { this.questionText = questionText; this.correct = correct; this.distractors = distractors; } }
Lecture 5: Review A List of Questions Question[] qList = { new Question( "Which property of Button would you change text color?", new String[]{"Color","BackgroundColor","Left","Top","Style"}, 0) ,new Question( "How do you make the cell td span two columns.", new String[]{"td.RowSpan = 2;","td.ColumnSpan = 2;", "td.ColumnSpan(2);", "td.Column.Span = 2; ", "ColumnSpan(td,2);"}, 1) };
Lecture 5: Review Display a Question • We can display a question in a Panel. The ToPanel method of Question returns a Panel. public Panel ToPanel() { Panel p = new Panel(); Label q = new Label(); q.Text = this.questionText; p.Controls.Add(q); RadioButtonListrbl = new RadioButtonList(); for (inti = 0; i < distractors.Count(); i++) rbl.Items.Add(distractors[i]); p.Controls.Add(rbl); return p; }
Lecture 5: Review Scoring • To score the test we need to “traverse” the list of radio buttons. • But... The RadioButtonList control has no record of the correct answer. • Controls such as Radio button items and DropListItems can have two values: • Text – the users see this value • Value – this is returned to the programmer.
Lecture 5: Review Setting the value • We can set the value when we create the radio button item. • From ToPanel() delete this: rbl.Items.Add(distractors[i]); • Insert this: ListItem li = new ListItem(distractors[i], (i == correct) ? "1" : "0"); rbl.Items.Add(li);
Lecture 5: Review Traversing the Page int score = 0; for (inti = 0; i < form1.Controls.Count; i++) if (form1.Controls[i] is Panel) { Panel p = (Panel)form1.Controls[i]; foreach (Control c in p.Controls) if (c is RadioButtonList) { RadioButtonListrbl = (RadioButtonList)c; if (rbl.Text == "1") score++; } }
Lecture 5: Review Next Week • You will see the coursework. This is a longer task that you must complete on your own. • The lectures will include: • Reading from a database • Using JavaScript to have “client-side” processing.