Use ASP + list to bind controls (bottom)

Source: Internet
Author: User

DataGrid3

DataGrid3 is built on DataGrid2 by adding visual formatting and content formatting.

From DataGrid3.aspx:

<%@ Page language = "C #" src = "DataGrid. cs" inherits = "Samples. Data
GridPage "%> 〉
...

<Asp: DataGrid runat = server id = "titlesGrid"
AutoGenerateColumns = "false"
Width = "80%"
BackColor = "White"
BorderWidth = "1px" BorderStyle = "Solid" CellPadding = "2" Cell
Spacing = "0"
BorderColor = "Tan"
Font-Name = "" Font-Size = "8pt"> "〉
<Property name = "Columns"> "〉
<Asp: BoundColumn headerText = "Title" DataField = "title"/> "/〉
<Asp: BoundColumn headerText = "Author" DataField = "au_name"/> "/〉
<Asp: BoundColumn headerText = "Date Published" DataField =
"Pubdate"
DataFormatString = "{0: MMM yyyy}"/> }"/〉
<Asp: BoundColumn headerText = "Price" DataField = "price"
DataFormatString = "{0: c}"> }"〉
<Property name = "ItemStyle"> "〉
<Asp: TableItemStyle HorizontalAlign = "Right"/> "/〉
</Property> 〉
</Asp: BoundColumn> 〉
</Property> 〉

<Property name = "headerStyle"> "〉
<Asp: TableItemStyle BackColor = "DarkRed" ForeColor = "White"
Font-Bold = "true"/> "/〉
</Property> 〉
<Property name = "ItemStyle"> "〉
<Asp: TableItemStyle ForeColor = "DarkSlateBlue"/> "/〉
</Property> 〉
<Property name = "AlternatingItemStyle"> "〉
<Asp: TableItemStyle BackColor = "Beige"/> "/〉
</Property> 〉
</Asp: DataGrid> 〉

This. aspx file displays the same DataGrid Control declaration as before, and sets various
Style attributes. This will lead to a more visually attractive representation. You do not need to perform any operations on the code
Use the same files with code support as in the previous example.

Because it is obtained from WebControl, The DataGrid Control inherits
Style attributes such as Width, BackColor, BorderStyle, and Font. Name. In addition,
The DataGrid provides attributes such as CellPadding, which are table-specific. This
These attributes allow you to customize controls in general.

The Declaration also displays the set project styles, such as headerStyle and Alternating.
ItemStyle. These styles control the appearance of their corresponding projects. Note that
. The options are the same as the general project's foreground because their style is
The combination of AlternatingItemStyle and ItemStyle. Finally, this example uses the right alignment
The text in the price column shows how to set a style for a specific column.

The DataGrid also allows you to format text in its cells. This is done by setting Bound
The DataFormatString attribute value of Column is complete. This column uses its format description for formatting
The content of the cell in String. Format. This property can be formatted with the format type (such as date or currency)
Preset or add any content together. In addition, the current page's CultureInfo is taken into account for formatting.
And request, so it also supports regionalization. If the format is not specified, use the ToString
Method.

DataGrid4

DataGrid4 describes how to use the SelectedIndexChanged event
Select in the DataGrid.

Captured from DataGrid4.aspx:

<% @ Page language = "C #" src = "DataGrid4.cs" inherits = "Samples.
DataGrid4Page "%> 〉
...

<Asp: DataGrid runat = server id = "titlesGrid"
AutoGenerateColumns = "false"
Width = "80%"
BackColor = "White"
BorderWidth = "1px" BorderStyle = "Solid" CellPadding = "2"
CellSpacing = "0"
BorderColor = "Tan"
Font-Name = "" Font-Size = "8pt"
DataKeyField = "title_id"
OnSelectedIndexChanged = "OnSelectedIndexChangedTitlesGrid"> "〉
<Property name = "Columns"> "〉
<Asp: ButtonColumn Text = "Select" Command = "Select"/> "/〉
<Asp: BoundColumn headerText = "Title" DataField = "title"/> "/〉
<Asp: BoundColumn headerText = "Author" DataField = "au_name"/> "/〉
<Asp: BoundColumn headerText = "Date Published" DataField =
"Pubdate"
DataFormatString = "{0: MMM yyyy}"/> }"/〉
<Asp: BoundColumn headerText = "Price" DataField = "price"
DataFormatString = "{0: c}"> }"〉
<Property name = "ItemStyle"> "〉
<Asp: TableItemStyle HorizontalAlign = "Right"/> "/〉
</Property> 〉
</Asp: BoundColumn> 〉
</Property> 〉

<Property name = "headerStyle"> "〉
<Asp: TableItemStyle BackColor = "DarkRed" ForeColor = "White"
Font-Bold = "true"/> "/〉
</Property> 〉
<Property name = "ItemStyle"> "〉
<Asp: TableItemStyle ForeColor = "DarkSlateBlue"/> "/〉
</Property> 〉
<Property name = "AlternatingItemStyle"> "〉
<Asp: TableItemStyle BackColor = "Beige"/> "/〉
</Property> 〉
<Property name = "SelectedItemStyle"> "〉
<Asp: TableItemStyle BackColor = "PaleGoldenRod" Font-Bold =
"True"/> "/〉
</Property> 〉
</Asp: DataGrid> 〉
...
<Asp: Label runat = server id = "selectionInfoLabel" Font-Name = ""
Font-Size = "8pt"/> "/〉

In this. aspx file, the SelectedIndexChanged event of the DataGrid is registered
An event handler. This event handler is implemented in files supported by code. Already
Add a "Select" ButtonColumn in the column set to make the DataGrid
Each item represents an additional column containing the Select button. SelectedItem
Style. This style is used to Visually differentiate selected items. Finally, the DataGrid's
DataKeyField attribute. This field is placed in the DataKeys set of the DataGrid.
Files with code support are used.

DataGrid4.cs:

Namespace Samples {
...

Public class DataGrid4Page: Page {
Protected DataGrid titlesGrid;
Protected Label selectionInfoLabel;

Public ICollection GetTitlesList (){
// Retrieve the title column from the DataSet cached in the application state
Table.
DataSet titlesDataSet = (DataSet) Application ["Titles
DataSet "];

If (titlesDataSet! = Null ){
Return titlesDataSet. Tables ["Title"]. DefaultView;
}
Else {
Return null;
}
}

Private void LoadTitlesGrid (){
// Retrieve data from the database
ICollection titlesList = GetTitlesList ();

// Set the data source of the control and reset its selection,
TitlesGrid. DataSource = titlesList;
TitlesGrid. SelectedIndex =-1;

// And make the control use this data source to build its project
TitlesGrid. DataBind ();

// Update the selected title information
UpdateSelectedTitleInfo ();
}

Protected override void OnLoad (EventArgs e ){
Base. OnLoad (e );

If (! IsPostBack ){
// Request this page for the first time
LoadTitlesGrid ();
}
}

// Process the OnSelectedIndexChanged event of the DataGrid
Protected void OnSelectedIndexChangedTitlesGrid (object
Sender,
EventArgs
E ){
UpdateSelectedTitleInfo ();
}

Private void UpdateSelectedTitleInfo (){
// Obtain the selected index
Int selIndex = titlesGrid. SelectedIndex;
String selTitleID = null;
String selectionInfo;

If (selIndex! =-1 ){
// Display the key fields of the selected title
SelTitleID = (string) titlesGrid. DataKeys [selIndex];
SelectionInfo = "ID of selected title:" +
SelTitleID;
}
Else {
SelectionInfo = "No title is currently selected .";
}

SelectionInfoLabel. Text = selectionInfo;
}
}
}

This. cs file contains the SelectedIndexChanged event processing and
The logic for displaying the ID of the selected title. The DataGrid handles command events by including
Triggered by a button in the project. It recognizes the standard command "Select", which causes it to change
Its SelectedIndex attribute, and the code that notifies the user of this change by triggering this event.

In the process of implementing the event processing program, the sample code calls UpdateSelectedTitle
Info method. This method is used to display information about the selected title. In this example, it is the title ID.
In a more practical solution, this ID can be used as a chain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.