Official station: http://quickstarts.asp.net/Futures/default.aspx
This article gives a more in-depth introduction to the use of dynamic data controls (including custom methods), including the following parts:
- Locate Database
- Ing to tables or views in the database
- Other Dynamic Data Controls
- Use extended mode to customize display styles
- Select columns to display
- Create custom Columns
- Customize the display style of a row
- Custom Data sources
Locate Database
The Dynamic Data Control selects a database in the following order:
- If Web. config contains the <dynamicdatacontrols> definition and <dynamicdatacontrols> declares the database connection string, the dynamic data control uses the database:
<dynamicDataControls showAllTables="true"
dataLayerType="Microsoft.Web.DynamicDataControls.SqlDataLayer"
connectionString="TASKSConnectionString" >
<nameMap>
</nameMap>
</dynamicDataControls>
- If the web. config does not contain the <dynamicdatacontrols> section, or <dynamicdatacontrols> does not declare a database connection string, and the Web. config exactly contains one and only one database connection string, so the Dynamic Data Control will use this database. If Web. config contains multiple database connection strings, you must declare the database connection string in <dynamicdatacontrols>.
- If no database connection string exists in Web. config, but app_data contains a database file, the dynamic data control uses the database. If app_data contains multiple database files, the database connection string must be declared.
Ing to tables or views in the database
For example, if there is a table or view named MERs in the database:
The Dynamic Data Control defined in mers MERs. aspx automatically maintains data in the customers table.
If you need to use multiple pages to maintain the MERs table, you can name it like this. Note: List. aspx and details. aspx have special meanings. Used to display data in a list or in detail:
~/Customers/list.aspx
~/Customers/details.aspx
~/Customers/anotherCustomView.aspx
However, we can also overwrite this default setting. For example, <dynamicdatacontrols>:
<dynamicDataControls listView="MyList" detailsView="MyDetails">
<nameMap>
<add table="customers" pathPrefix="~/MyCustFolder" />
</nameMap>
</dynamicDataControls>
Map the MERs table to the mycustfolder folder instead of the default customers folder. In addition, the names of list. aspx and details. aspx should also be changed to mylist. aspx and mydetails. aspx.
Other Dynamic Data Controls
Currently, ASP. NET Futures provides the following dynamic data controls:
- Dynamicautodata: Display all the following controls on a single page. ASP. NET futures-Dynamic Data Control: Getting Started This article uses this control as an example program.
- Dynamiclist: Display the table as a gridview.
- DynamicdetailsDisplay the table as detailsview, that is, only one row of data in the table is displayed.
- Dynamicfilter: Allows users to filter data.
- DynamicinsertAllows you to insert data.
- Dynamicnavigator: Allows you to navigate between all current dynamic data control pages.
- Dynamicrsslink: Provides data in RSS mode.
Use extended mode to customize display styles
To control the display style of data, simply use the extended mode of the Dynamic Data Control:
<asp:DropDownList runat="server" ID="ddl"BackColor="Yellow" Font-Italic="true" /><asp:DynamicFilter runat="server" ColumnName="Category" ControlID="ddl" />
In this way, you can first define the data display style, and then use the Dynamic Data Control to "fill in" the data.
Select columns to display
Override the getcolumns () method to control which columns in the table are displayed by the dynamic data control, for example:
public override IEnumerable GetColumns()
{
return new object[] { "Description", "Complete" };
}
Create custom Columns
To create a custom column, you only need to override the getcolumns () method:
public override IEnumerable GetColumns()
{
return new object[] {
"Category",
new DynamicDataColumn(
delegate { return EvalS("ShipName") + " (" +
EvalS("ShipAddress") + ")"; })
};
}
However, the title of the newly added column is "M m column" by default ". To specify a meaningful name, use the following code:
public override IEnumerable GetColumns()
{
return new object[] {
"Category",
new DynamicDataColumn(
"Description (Complete)",
delegate { return EvalS("ShipName") + " (" +
EvalS("ShipAddress") + ")"; }
)
};
}
The above code can also use a Custom column title.
Customize the display style of a row
To customize the display style of a row, you only need to override the initrow () method:
public override void InitRow(GridViewRow row)
{
if (EvalS("ShipName").IndexOf("Buy") > -1)
{
row.BackColor = Color.Yellow;;
}
}
Custom Data sources
Override the initdatasource () method to complete the custom data source function:
public override void InitDataSource(IDataSource dataSource)
{
dataSource.FilterExpression = ...;
}