Lblsalary note we set these ids as we will be

This preview shows page 24 - 26 out of 26 pages.

lblSalary . (Note: We set these IDs as we will be accessing the control values from the C# code . You can set the button ID and the other two labels’ ID properties as well, but we won’t be accessing them from our code . ) 14 . In Design view, add a C# event handler for the button-click event by double-clicking on the Calculate Salary button . This will place you in the page code behind file the editor . (Remember that ASP . Net pages have a file containing the HTML markup with an extension of . aspx and a C# ‘code behind’ file with an extension of . aspx . cs . ) This is the code that should be displayed: (If you changed the ID of the button, it will be a different method name . ) Click here for text file of this image . This code will be called each time the user presses the button . It is important to remember that code in the code behind page executes on the server – not on the user’s browser . This means that when the button is pressed, the page is submitted back to the web server and is processed by the ASP . Net application server on the web server . It is this code (between the { and } in this method) that will execute on the server . Once it is done executing the page will be sent 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 page . 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 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 variable . Create a third variable of type Double and set its value to the annual hours variable value multiplied by the rate double variable value . Take this resulting value and convert it to a string (text), and update the lblSalary Text property with this new string .
Hints: A control’s property can be accessed by simply using the control ID followed by a . followed by the name of the property . For example, the value stored in the Text property of the txtAnnualHours control can be accessed by using this: txtAnnualHours . Text . Text properties on controls are of type string . To convert a string to a Double you can use the Convert class . If we had a string variable called str1 and a double variable called myNumber, the C# code to convert this would be as follows: Click here for text file of this image . When converting from one type to another, we are assuming that the value stored in the type being converted is compatible with the type we are converting to . In the example above, if the value stored in str1 was not type compatible with a Double (for example “tiger”) an error would be raised . To set the value of a control on a web form, you can access the control and set the property directly . If I had a label control called lblCar and I wanted to update the text that was displayed in the label, I could do something like this: Click here for text file of this image .

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture