Chapter 5: Web Server Controls
Label control’s Hot key ==> AccessKey=”N” AssociatedControlID=”TextBox1”
Literal Control ==> Mode=”Encode”, will encode the output before displaying
To enable AutoPostback, enable the property for that control (drop down list, textbox tec.)
Button server Control:
You can have two buttons that has the same OnCommand method, but different name, such as
In the OnCommand code, you can check the command name to decide what code to run:
If you have other parameters to add to the button, use CommandArgument property. in the code behind, you check the value of e.CommandArgument.
Chapter 6: Validation server Controls
To use unobstrusive validation, need jQuery; also, configure, either
(2) or, in global.asax
Note: The data-* attributes are only rendered when unobstrusive validation is turned one.
When using requiredfieldvalidator to validate a drop down, the the InitialValue to the first value (default selected) of the drop down, such as this one:
Use CompareValidator to ensure the input is an integer.
You can use “Currency”,”Date”, “Double”, “Integer”, String”
Use CompareValidator to validate against a value
input has to be an integer >= 18
Must be an integer between 30 and 40
To turn off client-side validation, set the validation control’s property EnableClientScript to false.
if you want to show images (instead of text) as error message, you can edit the Text property as this one shows:
You get this:
Use Validation Groups to validate different groups of controls on the same form
Chapter 7: User and Server Controls
Load user control into form programmatically
To access properties and methods that the user control exposes, you need to cast the control from the generic control type to its actual type.
Custom Server Control – Creating a test one!!
User Control is a great way of encapsulating functionality in one unit.
The custom control needs to be created as a new project, not a new item.
Create a ASP.NET Server Control project, compile it, and add another web project, compile the solution, so you have:
After that you should see your server control in the toolbox:
If you don’t, right click the white space in toolbox, and check “Show All”, you should see a lot more controls in the toolbox, including your custom one. uncheck the “Show All”, and most will be gone, but your custom control will stay. Now you can drag your custom control to your web form.
Chapter 8: Data Binding
Once you set up a data source, you can configure the data source mode to either DataSet or DataReader
Default is OverwriteChanges – If changed to CompareAllValues: does not update the data if something has changed since the data is retrieved for update.
To detect and process data access error: In DataSource’s Updated event.
To use Entity Data Source
(1) Add a Entity Data Model toy our project, configure it to map to tables/store procs/views of a database.
(2) Add a Entity Data Source and use that connection as a connection, and configure it.
Query Extender – works with any data source control that implements the iQueryableDataSource interface. That includes LinqDataSource and EntityDataSource.
(1) Drag it to the designer
(2) Use the EntityDataSource’s id as the “TargetControlID” of the QueryExtender.
he Query Extender further filter the query results to the condition specified
XMLDataSource Control: Binding XML in memory or on a physical drive.
ObjectDatSource – bind directly to middle-layer classes or business objects.
Enabling caching by changing the property in the data source. Note that SQLDataSource caching is available only when the DatsSourceMode is DataSet.
A good control that can be used: Chart
Chapter 9: Model Binding
MVC was only available in MVC, but now also available in Web Form.
In GridView, you specify the optional ItemType=”Customer”, to get the intellisense.
If you’re expecting a parameter from the QueryString:
System.Web.ModelBinding.QueryString is a ValueProvider
ValueProvider: Form, Control, QueryString, Cookie, Profile, RouteData, Session
Example: A drop down and a GridView
The GridView is to be refreshed upon selection of an item in Drop Down: (Drop down’s AutoPostBack is true, so GridView will refreshed during the postback)