22.05.2017 Views

CIS 407 DeVry iLab 1 of 7 latest

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This code will be called each time the user presses the button. It is important to remember that code in the code behind<br />

page executes on the server – not on the user’s browser. This means that when the button is pressed, the page is<br />

submitted back to the web server and is processed by the ASP.Net application server on the web server. It is this code<br />

(between the { and } in this method) that will execute on the server. Once it is done executing the page will be sent<br />

back to the browser. Any changes we make to the page or controls on the page will be shown to the user in the updated<br />

page.<br />

15. In this method, add code that will get the text in the txtAnnualHours text box, convert it to a Double, and store it in<br />

a double variable. Add code that will get the text from the txtRate text box, convert it to a Double, and store it in another<br />

variable. Create a third variable <strong>of</strong> type Double and set its value to the annual hours variable value multiplied by the<br />

rate double variable value. Take this resulting value and convert it to a string (text), and update the lblSalary Text<br />

property with this new string.<br />

Hints:<br />

A control’s property can be accessed by simply using the control ID followed by a . followed by the name <strong>of</strong> the property.<br />

For example, the value stored in the Text property <strong>of</strong> the txtAnnualHours control can be accessed by using this:<br />

txtAnnualHours.Text. Text properties on controls are <strong>of</strong> type string.<br />

To convert a string to a Double you can use the Convert class. If we had a string variable called str1 and a double<br />

variable called myNumber, the C# code to convert this would be as follows: Click here for text file <strong>of</strong> this image.<br />

When converting from one type to another, we are assuming that the value stored in the type being converted is<br />

compatible with the type we are converting to. In the example above, if the value stored in str1 was not type compatible<br />

with a Double (for example “tiger”) an error would be raised.<br />

To set the value <strong>of</strong> a control on a web form, you can access the control and set the property directly. If I had a label<br />

control called lblCar and I wanted to update the text that was displayed in the label, I could do something like this:<br />

Click here for text file <strong>of</strong> this image.<br />

Note that following code would be incorrect and cause an error:<br />

Click here for text file <strong>of</strong> this image. lblCar is a Label – it isn’t a string so we can’t assign a string directly to it, but we<br />

can assign a string directly to the Text property <strong>of</strong> the label. All <strong>of</strong> the base types in C# (double, int etc) have a ToString()<br />

method you can call. If you had a double variable that you wanted to convert to a string and set that string to my label’s<br />

text, you would do the following:<br />

Click here for text file <strong>of</strong> this image.<br />

This would take whatever value was stored in the myNumber Double and convert it to a string. To add a $ to output<br />

you can use string concatenation in C# like this:<br />

Click here for text file <strong>of</strong> this image.<br />

16. Set your new form as the start page by clicking once on the form name in the Solution Explorer and then rightclicking<br />

on the form name and selecting “Set as Start Page.” You can now test your application and make sure it works<br />

correctly as you did with the Hello World form above. You can switch back and forth between which form runs when<br />

you run your application by setting the different forms as the start page.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!