Asp.net|datagrid|web long ago wanted to write something about datagrid/datalist, but has always been, on the one hand, the study is not deep, on the other hand, the total feel no writing, a drag and drop, from the beginning of the idea has been more than a year.
Datagrid/datalist in the asp.net of importance, I must not stress again, where the table type of data display, most of the use of these two controls (of course, if who still write like the ASP asp.net, then I have no way), so, Everyone may have their own understanding, this article, is a kind of a tip for everyone to do a foreshadowing.
First, the method
1, DataBind
Very simple, most commonly used method. Binding data. There is only one point to note: After this method has been executed, the DataGrid (because the DataGrid and DataList are very similar, so the following introduction is for the DataGrid, but not far from the DataList) all the controls that display bound data. will display the data in the DataSource, and the remaining controls will be initialized to the state of the design in. aspx.
Second, the property
1, DataSource
Where there is databind, there should be datasource. If the datasource is not specified and the DataBind is executed, the DataGrid will not display anything.
DataSource is usually a dataset, a DataTable, or a DataView. You can, of course, bind DataReader or other classes that implement IEnumerable.
2, Datakeyfield,datakeys
After you have positioned a row in the DataGrid, you definitely want to know where this line is in the datasheet, at least five ways to do this, and setting the DataKeyField of the DataGrid is one of these methods.
DataKeyField is generally set to the unique field of the datasheet (otherwise meaningless), and you can get the value of the key field in the row by DataKey.
DataKeys is a collection of datakey that reads the datakey of the corresponding rows through the index of the rows.
3, Edititemindex,selectedindex,currentpageindex,selecteditem
These attributes are well understood, the name to know what the meaning, it should be noted that the EditItemIndex or currentpageindex after the need to perform the DataBind method (of course, the previous mentioned, also need to set DataSource).
4, Columns
There's nothing to explain, columns is columns, a collection of columns, you can set properties for columns, including visible, HeaderText, Footertext, SortExpression, and so on.
Serious note: Automatically generated columns that are not included in the columns. Only columns that are declared in. aspx that have been added to the code are included in the columns.
5, Items
As the saying goes, the last is the most important, the items as the last attribute to introduce, formally based on such a reason.
Items are a collection of datagriditem that can traverse the DataGridItem that displays data in the current DataGrid.
5.1, DataGridItem
Each DataGridItem is a row displayed in the DataGrid, which includes:
Header portion of the header DataGrid control
Item in the DataGrid control
AlternatingItem alternating items in the DataGrid control
SelectedItem the selected item in the DataGrid control (set by SelectedIndex, read by SelectedItem property or Items[selectedindex)
EditItem the edited item in the DataGrid control (set by EditItemIndex, read by Items[edititemindex])
Separator separator between items in the DataGrid control
Footer the footer section of the DataGrid control
Pager page Selection section of the DataGrid control
Note that the Items property of the DataGrid does not contain the header, Footer, and pager of the three categories of DataGridItem.
Properties of 5.1.1, DataGridItem
itemindex--gets the index of rows in items
itemtype--returns the type of row, which is the header, Item 、...、 Pager listed above
cells--returns all the TableCell contained in the row (whether it is displayed or automatically generated, whether visible or hidden), the text displayed in the cell, the contained controls can be read through the TableCell
Serious Note: only BoundColumn columns and auto-generated columns can be used to read the displayed text through the Tablecell.text property. HyperLinkColumn, ButtonColumn, editcommandcolumn all need to convert the target control to the appropriate control.
Like what:
Suppose the first column of the DataGrid is declared as follows
<asp:hyperlinkcolumn datatextfield= "au_id" headertext= "au_id" datanavigateurlfield= "au_id" datanavigateurlformatstring= "Edit.aspx?id={0}" ></asp:HyperLinkColumn>
You can use it when reading:
Items[0] Represents the first row, Cells[0] represents the first column, Controls[0] represents the first control in the cell (and only this control can be used)
HyperLink link = (HyperLink) datagrid1.items[0]. Cells[0]. Controls[0]);
Response.Write (link. Text);
As for the template column (TemplateColumn), of course, can also pass datagrid1.items.cells[j]. Controls[n] to get, and then convert to the original control type again, but there is a better way to use FindControl to find the control.
FindControl is a System.Web.UI.Control method that can be used to find child controls based on the child control ID
Like what:
Suppose a column of a DataGrid is declared as follows
<asp:TemplateColumn>
<ItemTemplate>
<asp:textbox runat= "Server" id= "txtID" text=<%# DataBinder.Eval (Container.DataItem, "au_id")%>>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
Read method:
TextBox txt = (textbox) datagrid1.items[1]. FindControl ("txtID");
Response.Write (txt. Text);
Note: There is no cell in the DataList.
III. Events
1, ItemCommand, CancelCommand, DeleteCommand, EditCommand, UpdateCommand
In the DataGrid, the event that is executed after clicking button or LinkButton depends on the commandname of the button. In fact, the main one is ItemCommand, and the back four are just a small part of ItemCommand,
For example, the commandname of a button is "Cancel", and when returned, the first execution is the ItemCommand event, then the CancelCommand event.
2, PageIndexChanged
If your DataGrid is paging, this event will be triggered when you click 1, 2, 3, or < > on the pager on the DataGrid.
In this case, you can use E. NewPageIndex to read the page you want to change, and then assign it to the CurrentPageIndex property of the DataGrid, and don't forget to set up DataSource and perform DataBind.
Note: DataList does not have this event, if you need to paging in the DataList, you can read the data for a period of time, and then bind the current segment of the data to the DataList.
3, itemdatabound,itemcreated
The first thing to say is the timing of these two events.
ItemDataBound, as long as the DataBind method is executed, the event is immediately aroused.
itemcreated, if the page is the first access (Page.IsPostBack = False), the ItemCreated event is fired first when the DataBind is executed, that is, after the DataBind is executed, First the header line is built with itemcreated, then the header row is bound with ItemDataBound, then the first line is built with ItemCreated, then the itemdatabound is used to bind the first row, In other words, itemcreated and ItemDataBound are executed alternately.
When the page returns, the itemcreated is also executed
Event, before Page_Load, but this time no longer executes the ItemDataBound event.
So, if you want to add any controls dynamically in the DataGrid, you need to be in the ItemCreated event, not in the ItemDataBound event.
Iv. Code Snippets
1, the DataGrid display double table head
Assuming your DataGrid has three columns, now you want to use the first two columns as "big Class 1" and the third column as "Big Class 2", now you can add the following code to the ItemDataBound event:
if (E.item.itemtype = = Listitemtype.header)
{
E.item.cells[0]. ColumnSpan = 2;
E.item.cells[0]. Text = "Large class 1</td><td> Large class 2</td></tr><tr><td>" + E.item.cells[0]. Text;
}
This method allows you to add any new rows.
2, set the bound column or automatically generate column edit box width
Please add the code to your ItemDataBound event:
if (E.item.itemtype = = ListItemType.EditItem)
{
for (int i = 0; i < E.item.cells.count; i++)
{
TextBox txt = (textbox) e.item.cells.controls[0];
Txt. Width = Unit.pixel (50);
}
}
3. Handling of DropDownList events in the DataGrid
DropDownList has no commandname attribute, so you can't use the ItemCommand event, but you can try this:
DropDownList control added to the template column in the DataGrid
<asp:dropdownlist runat= "Server" id= "DDL" autopostback= "True" onselectedindexchanged= "ddl_selectedindexchanged" />
Then you add a function to the. aspx.cs
protected void Ddl_selectedindexchanged (object sender, System.EventArgs e)//must be declared as protected or public, not private.
{
You can add other code here
}
3.1. How do I get the value of other cell in the above incident?
We know that the DataGrid is completely a table-structured control, that the DataGrid contains DataGridItem, and that each datagriditem contains TableCell, so we can, in a control in TableCell, Use the control of the parent to get TableCell, and then use TableCell's parent, you can get DataGridItem.
protected void Ddl_selectedindexchanged (object sender, System.EventArgs e)//must be declared as protected or public, not private.
{
DropDownList DDL = (DropDownList) sender;
TableCell cell = (TableCell) DDL. Parent;
DataGridItem item = (DataGridItem) cell. Parent;
Response.Write (item. Cells[0]. Text);
}
4. How to get the control in header, Footer, pager
Method one: In ItemCreated or ItemDataBound, the specific code is not written more
Method Two: Iterate through all of the DataGrid's item (note, not traversing the item under Datagrid1.items)
foreach (DataGridItem item in Datagrid1.controls[0]. Controls)
{
if (item. ItemType = = Listitemtype.header)
{
With item. FindControl find the appropriate control
}
}
You may notice that there is a datagrid1.controls[0 here. Controls, which means that, under DATAGRID1, there is a child control, the child control is a datagridtable type, and the following is the DataGridItem set
In DataList, the following child controls are directly datalistitem without a table:
foreach (DataListItem item in Datalist1.controls)
{
//....
}