Last Updated: 2001
Data-binding |
The <%# ....%> indicates a data source. A data source could be a page property, an expression, function call or any object that implements the ICollection / IEnumerable / IListSource interface (i.e. ArrayList, Hashtable, DataView, DataReader, etc.). Values are not substituted until the DataBind() method of the control is called. DataBind() is recursive, so calling it for a container object results in all the controls within that container calling their DataBind() methods. As such, DataBind() is normally called only once, called for the Page object and called in the Page_Load event. The DropDown, DataGrid, DataView, ListBox and HTMLSelect bind to a datasource using the datasource parameter. ?? Radio buttons? Examples:
The result of a <%# ... %> substitution must be a character string. Rather than writing expressions such as <%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["iField5"])%> .Net supports the equivalent syntax <%# DataBinder.Eval(Container.DataItem, "iField5", "{0:c}") %> (actually this isn't strictly equivalent as the first version executes much faster as the second version uses late binding and reflection). The parameters for DataBinder.Eval are Container, Field Name[, Format string]. |
SQL |
Equivalent to using System.Data.SQL.... is <%@ Import Namespace="System.Data...." %> DataSet objects give "disconnected views" of the data. Reader objects maintain a connection to the server. DataSets can also be filled from XML using a FileStream object and the various ReadXML... functions. Example:
SqlDataReader uses SQL's good-old TDS (Tabular Data Stream) protocol directly, so (a) should give good performance and (b) is forward only. There is a potential security issue with using connection strings in that users could embed additional commands in the username and/or password fields. For example entering a user name of "sa;database=someOtherDB". Parameterised views can be created using the @placeholder syntax. For example:
Note: Placeholders are not limited to SELECT statements, but can also be used, for example, in the VALUES clause of a INSERT statement. Useful Connection Properties
Note that (a) "yes" and "no" can be used in place of "true" and "false" for boolean properties and (b) integer properties are set/read as strings. Calling a Stored ProcedureSet the SqlCommand object's CommandText property to the name of the stored procedure and it's CommandType property to CommandType.StoredProcedure. Parameters can be declared and passed in the normal manner (note: the parameters should not be included in the CommandText line ?? really true ??) |
DataGrid Control |
DataSource property accepts a IEnumerable, ICollection, DataSet or DataReader object. DataSource property is overloaded such that oDataGrid.DataSource = oDataSet.Tables("myTable"); equates to oDataGrid.DataSource = oDataSet; oDataGrid.DataMember = "myTable";. As ever, the DataBind() method must be called to expand the results. Example: oDataSet1.DataSource = oDataSet.Tables("table1"); All controls (i.e. everything descended from the Control object) have a property called EnableViewState which determines whether the state of the control is retained across subsequent HTTP requests (via a hidden field). In many cases it is unnecessary to save this information for DataGrids as you are likely to want to refresh the page (i.e. reissue & rebind the select) whenever the user hits refresh. To prevent the system recording the contents of the datagrid (it could be a large amount of data to transfer) only to have it replaced by your custom requery code, set this property to false. Editing in a GridA specific row in the DataGrid can be replaced with a series of text boxes by setting the DataGrid's EditItemIndex to the line number (set to -1 (the default) to prevent editing). The EditItemStyle property object can be used to change the appearance of the line being edited. Note: Changes to EditItemIndex & EditItemStyle do not take place until the next BindData() call. The OnEditCommand, OnCancelCommand and OnUpdateCommand function pointer properties of the DataGrid control can be set to name of a specific handler to be called upon each of those events. For example: oDataGrid.OnEditCommand = "myEditCommand". The handler prototype should be (Object sender, DataGridCommandEventArgs E). The DataKeyField property can be used to identify a column in the grid that can be referenced from the handler to identify the record the row relates to. Examples: (In HTML form) (In code) Column Formatting and ValidationUnlike automatically generated columns, BoundColumns can have properties set at an individual level, including the ReadOnly setting. Bound columns are created using the <asp:BoundColumn... /> tag in the columns section of the grid declaration. Properties valid for a BoundColumn are:
Example: <asp:DataGrid id="myGrid" runat="server" Displaying Different Control Types in View and Edit modeIndividual columns can be formatted using <asp:TemplateColumn> tag within the <Columns> tag. Within <TemplateColumn> <ItemTemplate> determined the normal display format and <EditItemTemplate> the display format when editing. For example (paraphrased from MS code):
Notes:
Button ColumnsA button column can be defined within the <Columns> clause using the <asp:ButtonColumn> tag. For example: <Columns> DataField can be used in place of Text to generate the button caption dynamically. Standard formatting can be applied to the data source using DataFormatString. When clicked the OnItemCommand handler identified in the DataGrid tag is called and passed the value of CommandName as one of the properties of the event args parameter. Note: It appears (the documentation is blatantly absence in this matter) that specifying a CommandName of Delete causes OnDeleteCommand to be run instead of OnItemCommand. Empirical testing If handlers are supplied for both OnItemCommand and OnDeleteCommand then the OnItemCommand handler is called first followed by the OnDeleteCommand handler. HyperLink ColumnsA hyperlink column can be defined within the <Columns> tag as follows: <Columns> A frame can be specified for the new page using the Target property. Dynamic hyperlinks can be generated using:
|
Repeater Control |
As per grids, this controls are primarily configured using the DataSource property and the DataBind() function. Unlike DataGrid, this control contain no formatting or layout information themselfs but instead rely on <ItemTemplate> sub-tags to enclose a batch of layout data that is to be repeated once per record. Header and footer information can be included within DataList / Repeater control, but it only displayed once. For example:
asp:Repeater supports OnItemCommand, but none of the other On...Commands that were supported by grids. As stated earlier, we can bind to anything supporting one of several interfaces. The exact type of Container.DataItem will be dependent on the collection (or whatever) we binded to. For example, it could be DataRowView or Person (for a fictional ArrayList of Person objects). Other sub-tags of interest include:
|
DataList Control |
Very similar to the Repeater control except control of formatting outside of the current "box" is allowed. For example, if you want to display multiple columns (where each "row" in each column is a different record) or to use the <div> tag, you need to use the DataList control.
DataList supports the <EditItemTemplate> sub-tag that allows another template to be used when in edit mode. DataList supports the On...Commands for Select, Edit, Update, Item and Cancel. |