asp.net datalist Usage _ Practical skills

Source: Internet
Author: User
Design Template:
Header
<HeaderTemplate>
</HeaderTemplate>
Footer
<FooterTemplate>
</FooterTemplate>
Data logging
<ItemTemplate>
</ItemTemplate>
<AlternatingItemTemplate> Alternating Display items
</AlternatingItemTemplate>
<SelectedItemTemplate> How to display when selected
</SelectedItemTemplate>
<EditItemTemplate> How to display when editing
</EditItemTemplate>
<SeparatorTemplate> Data Record Separator
</SeparatorTemplate>
Edit templates, which can be embedded in controls, binding data.
<ItemTemplate>
<table>
<tr>
<td><%# DataBinder.Eval (Container.DataItem, "shareholding name")%></td>
<td><%# DataBinder.Eval (Container.DataItem, "market capitalisation", "{0:n}")%></td>
<td><%# DataBinder.Eval (Container.DataItem, "net", "{0:n}")%></td>
</tr>
</table>
</ItemTemplate>
Set appearance
RepeatLayout property setting display mode
RepeatDirection Display Direction
Number of RepeatColumns columns
Event
The button that joins the stencil column will raise its click event to the ItemCommand event or set the CommandName
To respond to different events, such as set to: Edit, which causes EditCommand (), and so on.
Note: If set to: Select will cause selectedindexchanged and ItemCommand events
SelectedItemTemplate template; A control that adds details, and when the user selects the item, the selection template is displayed.
private void Datalist1_itemcommand (...)
{Switch (e.commandname)
{
Case "SELECT":
This. Datalist1.selectedindex=e.item.itemindex;
String s= (String) this. Datalist1.datakeys[e.item.itemindex];
The detailed data of the record is obtained here, as shown in the SelectedItemTemplate template.
Break
Case "Unselect":
This. Datalist1.selectedindex=-1;
Break
}
This. Datalist1.databind ()//Must be
}
EditItemTemplate Template
Edit:
This. Datalist1.edititemindex=e.item.itemindex;
This. Datalist1.databind ();
Update:
Get primary Key
String s= (String) this. Datalist1.datakeys[e.item.itemindex];
Get the controls in the stencil
TextBox box= (textbox) E.item.findcontrol ("TextBox1");
Update records
This. Datalist1.databind ();
Cancel:
This. Datalist1.edititemindex=-1;
This. Datalist1.databind ();
Delete Item
Check multiple records at once and delete them once
Copy Code code as follows:

foreach (DataListItem i in this. Datalist1.items)
{
BOOL Ischecked= ((checkbox) I.findcontrol ("Deletectr")). Checked;
if (ischecked)
{
String s= (String) this. Datalist1.datakeys[e.item.itemindex];
Delete operation}
}

Running custom DataList controls
Copy Code code as follows:

When you create any item in the DataList control
private void Datalist1_itemcreated (object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
Switch (e.item.itemtype)
{Case Listitemtype.header:
e.item.forecolor=color.red;
E.item.backcolor=color.black;
Break
Case ListItemType.Item:
E.item.backcolor=color.black;
Break
}
}
When an item in a template is bound by data binding, the last chance that data is displayed before the client is accessed
private void Datalist1_itemdatabound (object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if ((E.item.itemtype==listitemtype.header) | | (E.item.itemtype==listitemtype.item))
{
System.Data.Common.DbDataRecord drv=
(System.Data.Common.DbDataRecord) e.Item.DataItem;
if ((decimal) drv["Stock"]<1000)
{
e.item.forecolor=color.red;
}
}
}

Another way
Copy Code code as follows:

if ((E.item.itemtype==listitemtype.header) | | (E.item.itemtype==listitemtype.item))
{
DataRowView drv= (DataRowView) e.Item.DataItem;
String department= (String) drv["department"];
Switch (department)
{Case "sales Department":
E.item.backcolor=color.black;
Break
Case "Technology Department":
e.item.backcolor=color.red;
Break }
}

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.