1. Repeater is used to display data and ListView is used to operate data.
InsertItemTemplate and updateItemTemplate
** Eval (display data) and Bind (two-way binding: not only need to be presented, but also need to Bind data to the database)
ItemPlaceholderID: A placeholder that determines the placeholder and separates the header (top) from the tail (bottom ).
ItemTemplate: display function
Automatically Generated ListView to be adjusted
(1. Do not use inline methods when generating styles.
(2. You do not need to use <asp: Label> to display read-only data in half of the ItemTemplate, so you can directly output the data.
<% # Eval ("id") %>
(3. The LayoutTemplate must contain an ItempPlaceholderID Server Control.
(4) The position of the header in LayoutTemplate must be in Chinese. All fields in the template that do not need to be displayed must be deleted or changed.
2. Events
The process is the same as that of Repeater:
// First determine the data Row Type
E. Item. ItemType = ListViewItemType. DataItem
// Convert e. Item into ListViewDataItem to get DataItem
ListViewDataItem lvDataItem = (ListViewDataItem) e. Item;
DataRowView rowView = (DataRowView) lvDataItem. DataItem;
// Obtain a column
Var xRow = (... DAL. DataSet1.T _ UserRow) rowVIew. Row;
// Obtain the value of a column
XRow. Age, xRow. sName... etc.
3. Notes
(1) set the corresponding buttons, controls, and Validator as the child's ValidationGroup,
Prevent Validator from interfering with each other in different templates,
(2) Insert CausesValidation = "false" in the Cancel button so that when you insert and modify data
You can cancel the operation so that the operation does not affect each other even in the same group.
4. Add default values to InsertItemplate
// Enter the function in the ItemCreate attribute
If (e. Item. ItemType = ListViewItemType. InsertItem ){
TextBox AgeText = (TextBox) e. Item. FindControl ("AgeID ");
AgeText. Text = "20 ";
}
5. Primary Key Guid: insert to database
(1) ItemInserting attribute of ListView:
// Key-value pairs of data to be inserted into the database
E. values ["id"] = Guid. NewGuid ();
(2) ItemUpdateing attribute of ListView:
E. ItemIdex
E. OldValues // value before update
E. NewValues ["Age"] // updated value
E. Cancel = true; // Cancel illegal data insertion
ObjectDataSource
When binding IDs of The guid type
6. DropDrownList
(1,
// Items contained in DropDrownList
<Asp: ListItem value = "man"> male </asp: ListItem>
(2,
** Background code: During update
// Find the ListView
// ListView1.Item [e. ItemIndex]. FindControl ("ID ");
// It is a DropViewList
DropDrownList d = (DropDrownList) listView1.Item [e. ItemIndex]. FindControl ("ID ");
// Assign a value
E. NewValues = ["field"] = d. SelectedValue;
(3,
** Background code: displays the original data during editing.
// Data rows exist
If (e. Item. ItemType = ListVIewDataList. DataItem ){
// Obtain the control
DropDownList d = (DropDownLIst) e. Item. FindControl ("ID ");
If (d! = Null ){
// Obtain the bound data of this row
ListViewDataItem lv = (ListViewDataItem) e. Item;
DataRowItem row = (dataRowItem) lv. DataItem;
// If this row contains data
If (row! = Null ){
// Read the Row value of the database
Var myRow = (project name. DAL. DataSetUsers. T_Users) row. Row;
// Set the Row value of the read doll to the option in the drop-down menu
D. SelectedValue = myRow. field;
}
}
}
(4. Enabled = "false" cannot be used.
Link Management:
Effect:
Summary:
(1) Warning 1 element "ListView" is not a known element. The reason may be that the website has a compilation error or the web. config file is missing. E: \ code \ Projects \ WebSite_zzl01 \ link \ LinkUrl_Admin.aspx 39 10 links
(2. onLinkTypeChange (this, '"+ logoID. the ID passed to the front-end javascript is not the client ID, it will cause the display and hidden functions to be unable to implement, so add a myID
: LogoID. Attributes ["myid"] = logoID. ClientID; to pass Parameters
LinkUrl_Admin.aspx.cs
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Namespace links
{
Public partial class LinkUrl_Admin: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void listviewincluitemdatabound (object sender, ListViewItemEventArgs e)
{
// The ItemDataBound attribute of ListView1 is bound to each row.
// Display data
If (e. Item. ItemType = ListViewItemType. DataItem ){
DropDownList ddlsLinkType = (DropDownList) e. Item. FindControl ("ddlsLinkType ");
ListViewDataItem dataitem = (ListViewDataItem) e. Item;
DataRowView myrow = (DataRowView) dataitem. DataItem;
If (ddlsLinkType! = Null & myrow! = Null ){
Var sUrl = (link. ADL. DataSet1.T _ LinksRow) myrow. Row;
DdlsLinkType. SelectedValue = sUrl. sLinkType;
}
}
}
Protected void listviewincluiteminserting (object sender, ListViewInsertEventArgs e)
{
// Insert data
DropDownList ddlsLinkType = (DropDownList) e. Item. FindControl ("ddlsLinkType ");
E. Values ["sLinkType"] = ddlsLinkType. SelectedValue;
}
Protected void listviewincluitemupdating (object sender, ListViewUpdateEventArgs e)
{
// Update data
DropDownList ddlsLinkType = (DropDownList) ListView1.Items [e. ItemIndex]. FindControl ("ddlsLinkType ");
E. NewValues ["sLinkType"] = ddlsLinkType. SelectedValue;
}
Protected void listviewincluitemcreated (object sender, ListViewItemEventArgs e)
{
If (e. Item. ItemType = ListViewItemType. DataItem | e. Item. ItemType =
ListViewItemType. InsertItem ){
DropDownList ddlsLinkType = (DropDownList) e. Item. FindControl ("ddlsLinkType ");
TextBox logoID = (TextBox) e. Item. FindControl ("LogoUrlTextBox ");
If (ddlsLinkType! = Null & logoID! = Null ){
// Onchange is the select attribute in html
// OnLinkTypeChange is the jQuery function customized by the front-end javascript called by the background code.
LogoID. Attributes ["myid"] = logoID. ClientID;
DdlsLinkType. Attributes ["onchange"] = "onLinkTypeChange (this, '" + logoID. ClientID + "')";
If (ddlsLinkType. SelectedValue = "Text "){
LogoID. Style ["display"] = "none ";
}
}
}
}
}
}
LinkUrl_Admin.aspx
Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "LinkUrl_Admin.aspx.cs" Inherits = "link. LinkUrl_Admin" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Links </title>
<Script src = "Scripts/jquery-1.4.2.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Function onLinkTypeChange (urlType, logoID ){
If ($ (urlType). val () = "Text "){
$ ("Input: text [myid =" + logoID + "]"). hide ();
// $ ("#" + LogoID). hide (); // The Client id is not the client id.
// $ ("# Listviewdomainlogourltextbox"). hide (); // REAL id
}
Else {
$ ("Input: text [myid =" + logoID + "]"). show ();
// $ ("#" + LogoID). show ();
}
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ObjectDataSource ID = "objectperformance1" runat = "server"
DeleteMethod = "Delete" InsertMethod = "Insert"
OldValuesParameterFormatString = "original _ {0}" SelectMethod = "GetData"
TypeName = "link. ADL. DataSet1TableAdapters. T_LinksTableAdapter"
UpdateMethod = "Update">
<DeleteParameters>
<Asp: Parameter Name = "Original_ID" Type = "Int64"/>
</DeleteParameters>
<InsertParameters>
<Asp: Parameter Name = "SeoNo" Type = "Int32"/>
<Asp: Parameter Name = "SiteName" Type = "String"/>
<Asp: Parameter Name = "sLinkType" Type = "String"/>
<Asp: Parameter Name = "SiteUrl" Type = "String"/>
<Asp: Parameter Name = "LogoUrl" Type = "String"/>
</InsertParameters>
<UpdateParameters>
<Asp: Parameter Name = "SeoNo" Type = "Int32"/>
<Asp: Parameter Name = "SiteName" Type = "String"/>
<Asp: Parameter Name = "sLinkType" Type = "String"/>
<Asp: Parameter Name = "SiteUrl" Type = "String"/>
<Asp: Parameter Name = "LogoUrl" Type = "String"/>
<Asp: Parameter Name = "Original_ID" Type = "Int64"/>
</UpdateParameters>
</Asp: ObjectDataSource>
</Div>
<Asp: ListView ID = "ListView1" runat = "server" DataKeyNames = "ID"
Performanceid = "objectperformance1" InsertItemPosition = "LastItem"
Onitemdatabound = "listviewdomainitemdatabound"
Oniteminserting = "listviewdomainiteminserting"
Onitemupdating = "listviewdomainitemupdating"
Onitemcreated = "listviewincluitemcreated">
<EditItemTemplate>
<Tr style = "background-color: #999999;">
<Td>
<Asp: Button ID = "UpdateButton" runat = "server" CommandName = "Update" Text = "Update"/>
<Asp: Button ID = "CancelButton" runat = "server" CommandName = "Cancel" Text = "Cancel"/>
</Td>
<Td>
<Asp: TextBox ID = "SeoNoTextBox" runat = "server" Text = '<% # Bind ("SeoNo") %>'/>
</Td>
<Td>
<Asp: TextBox ID = "SiteNameTextBox" runat = "server"
Text = '<% # Bind ("SiteName") %>'/>
</Td>
<Td>
<Asp: DropDownList ID = "ddlsLinkType" runat = "server">
<Asp: ListItem Value = "Text"> Text </asp: ListItem>
<Asp: ListItem Value = "Pic"> image </asp: ListItem>
</Asp: DropDownList>
</Td>
<Td>
<Asp: TextBox ID = "SiteUrlTextBox" runat = "server" Text = '<% # Bind ("SiteUrl") %>'/>
</Td>
<Td>
<Asp: TextBox ID = "LogoUrlTextBox" runat = "server" Text = '<% # Bind ("LogoUrl") %>'/>
</Td>
</Tr>
</EditItemTemplate>
<EmptyDataTemplate>
<Table runat = "server"
Style = "background-color: # FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;">
<Tr>
<Td>
No data is returned. </Td>
</Tr>
</Table>
</EmptyDataTemplate>
<InsertItemTemplate>
<Tr style = "">
<Td>
<Asp: Button ID = "InsertButton" runat = "server" CommandName = "Insert" Text = "Insert" ValidationGroup = "Insert"/>
<Asp: Button ID = "CancelButton" runat = "server" CommandName = "Cancel" Text = "clear"/>
</Td>
<Td>
<Asp: TextBox ID = "SeoNoTextBox" ValidationGroup = "Insert" runat = "server" Text = '<% # Bind ("SeoNo") %>'/>
<Asp: RequiredFieldValidator ValidationGroup = "Insert" ID = "RequiredFieldValidator1" runat = "server" ErrorMessage = "*" ControlToValidate = "SeoNoTextBox">
</Asp: RequiredFieldValidator>
<Asp: compareValidator ValidationGroup = "Insert" ID = "CompareValidator1" runat = "server" ErrorMessage = "the serial number must be an Integer" ControlToValidate = "SeoNoTextBox" Operator = "DataTypeCheck" Type = "Integer">
</Asp: CompareValidator>
</Td>
<Td>
<Asp: TextBox ID = "SiteNameTextBox" ValidationGroup = "Insert" runat = "server" MaxLength = "50"
Text = '<% # Bind ("SiteName") %>'/>
<Asp: RequiredFieldValidator ValidationGroup = "Insert" ID = "RequiredFieldValidator2" runat = "server" ErrorMessage = "*" ControlToValidate = "SiteNameTextBox">
</Asp: RequiredFieldValidator>
</Td>
<Td>
<Asp: DropDownList ID = "ddlsLinkType" ValidationGroup = "Insert" runat = "server">
<Asp: ListItem Value = "Text"> Text </asp: ListItem>
<Asp: ListItem Value = "Pic"> image </asp: ListItem>
</Asp: DropDownList>
</Td>
<Td>
<Asp: TextBox ID = "SiteUrlTextBox" ValidationGroup = "Insert" runat = "server" Text = '<% # Bind ("SiteUrl") %>'/>
<Asp: RequiredFieldValidator ValidationGroup = "Insert" ID = "RequiredFieldValidator3" runat = "server" ErrorMessage = "*" ControlToValidate = "SiteUrlTextBox">
</Asp: RequiredFieldValidator>
</Td>
<Td>
<Asp: TextBox ID = "LogoUrlTextBox" ValidationGroup = "Insert" runat = "server" Text = '<% # Bind ("LogoUrl") %>'/>
</Td>
</Tr>
</InsertItemTemplate>
<ItemTemplate>
<Tr style = "background-color: # E0FFFF; color: #333333;">
<Td>
<Asp: Button ID = "DeleteButton" runat = "server" CommandName = "Delete" Text = "Delete"/>
<Asp: Button ID = "EditButton" runat = "server" CommandName = "Edit" Text = "Edit"/>
</Td>
<Td>
<Asp: Label ID = "SeoNoLabel" runat = "server" Text = '<% # Eval ("SeoNo") %>'/>
</Td>
<Td>
<Asp: Label ID = "SiteNameLabel" runat = "server" Text = '<% # Eval ("SiteName") %>'/>
</Td>
<Td>
<Asp: DropDownList ID = "ddlsLinkType" runat = "server" Enabled = "false">
<Asp: ListItem Value = "Text"> Text </asp: ListItem>
<Asp: ListItem Value = "Pic"> image </asp: ListItem>
</Asp: DropDownList>
</Td>
<Td>
<Asp: Label ID = "SiteUrlLabel" runat = "server" Text = '<% # Eval ("SiteUrl") %>'/>
</Td>
<Td>
<Asp: Label ID = "LogoUrlLabel" runat = "server" Text = '<% # Eval ("LogoUrl") %>'/>
</Td>
</Tr>
</ItemTemplate>
<LayoutTemplate>
<Table runat = "server">
<Tr runat = "server">
<Td runat = "server">
<Table ID = "itemPlaceholderContainer" runat = "server" border = "1"
Style = "background-color: # FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: verdana, Arial, Helvetica, sans-serif; ">
<Tr runat = "server" style = "background-color: # E0FFFF; color: #333333;">
<Th runat = "server">
</Th>
<Th runat = "server">
No. </th>
<Th runat = "server">
Website name </th>
<Th runat = "server">
Link Type </th>
<Th runat = "server">
Website </th>
<Th runat = "server">
Logo URL </th>
</Tr>
<Tr ID = "itemPlaceholder" runat = "server">
</Tr>
</Table>
</Td>
</Tr>
<Tr runat = "server">
<Td runat = "server"
Style = "text-align: center; background-color: # 5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif; color: # FFFFFF">
<Asp: DataPager ID = "DataPager1" runat = "server">
<Fields>
<Asp: NextPreviousPagerField ButtonType = "Button" ShowFirstPageButton = "True"
ShowLastPageButton = "True"/>
</Fields>
</Asp: DataPager>
</Td>
</Tr>
</Table>
</LayoutTemplate>
</Asp: ListView>
</Form>
</Body>
</Html>