数据绑定控件ListView
程序代码示例:
后台:
protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType ==ListViewItemType.DataItem) { ListViewDataItem listItem = (ListViewDataItem)e.Item; DataRowView dataView = (DataRowView)listItem.DataItem; var row = (WebApplication1.DateSource.DAL.UserDateSource.UserNameRow)dataView.Row; if (row.UserScore <70) { HtmlTableRow trRow = (HtmlTableRow)e.Item.FindControl("trRow"); trRow.Disabled = false; } } }
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListView.aspx.cs" Inherits="WebApplication1.ListView" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="WebApplication1.DateSource.DAL.UserDateSourceTableAdapters.UserNameTableAdapter"> <InsertParameters> <asp:Parameter Name="UserName" Type="String" /> <asp:Parameter Name="UserPsw" Type="String" /> <asp:Parameter Name="UserPicturePath" Type="String" /> <asp:Parameter Name="UserScore" Type="Int32" /> </InsertParameters> </asp:ObjectDataSource> <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> <AlternatingItemTemplate> <tr style=""> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </AlternatingItemTemplate> <EditItemTemplate> <tr style=""> <td> <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" /> </td> <td> <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' /> </td> <td> <asp:TextBox ID="UserPswTextBox" runat="server" Text='<%# Bind("UserPsw") %>' /> </td> <td> <asp:TextBox ID="UserPicturePathTextBox" runat="server" Text='<%# Bind("UserPicturePath") %>' /> </td> <td> <asp:TextBox ID="UserScoreTextBox" runat="server" Text='<%# Bind("UserScore") %>' /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <table runat="server" style=""> <tr> <td> 未返回数据。</td> </tr> </table> </EmptyDataTemplate> <InsertItemTemplate> <tr style=""> <td> <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" /> </td> <td> </td> <td> <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' /> </td> <td> <asp:TextBox ID="UserPswTextBox" runat="server" Text='<%# Bind("UserPsw") %>' /> </td> <td> <asp:TextBox ID="UserPicturePathTextBox" runat="server" Text='<%# Bind("UserPicturePath") %>' /> </td> <td> <asp:TextBox ID="UserScoreTextBox" runat="server" Text='<%# Bind("UserScore") %>' /> </td> </tr> </InsertItemTemplate> <ItemTemplate> <tr style=""> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </ItemTemplate> <LayoutTemplate> <table runat="server"> <tr runat="server"> <td runat="server"> <table ID="itemPlaceholderContainer" runat="server" border="0" style=""> <tr runat="server" style=""> <th runat="server"> ID</th> <th runat="server"> UserName</th> <th runat="server"> UserPsw</th> <th runat="server"> UserPicturePath</th> <th runat="server"> UserScore</th> </tr> <tr ID="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style=""> </td> </tr> </table> </LayoutTemplate> <SelectedItemTemplate> <tr style=""> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </SelectedItemTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" SelectCommand="SELECT * FROM [UserName]"></asp:SqlDataSource> <asp:ListView ID="ListView2" runat="server" DataSourceID="ObjectDataSource1" ondatabound="ListView2_DataBound" onitemdatabound="ListView2_ItemDataBound" onitemediting="ListView2_ItemEditing"> <AlternatingItemTemplate> <tr style="" runat="server" id="trRow"> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </AlternatingItemTemplate> <EditItemTemplate> <tr style=""> <td> <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" /> </td> <td> <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' /> </td> <td> <asp:TextBox ID="UserPswTextBox" runat="server" Text='<%# Bind("UserPsw") %>' /> </td> <td> <asp:TextBox ID="UserPicturePathTextBox" runat="server" Text='<%# Bind("UserPicturePath") %>' /> </td> <td> <asp:TextBox ID="UserScoreTextBox" runat="server" Text='<%# Bind("UserScore") %>' /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <table runat="server" style=""> <tr> <td> 未返回数据。</td> </tr> </table> </EmptyDataTemplate> <InsertItemTemplate> <tr style=""> <td> <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" /> <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" /> </td> <td> </td> <td> <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' /> </td> <td> <asp:TextBox ID="UserPswTextBox" runat="server" Text='<%# Bind("UserPsw") %>' /> </td> <td> <asp:TextBox ID="UserPicturePathTextBox" runat="server" Text='<%# Bind("UserPicturePath") %>' /> </td> <td> <asp:TextBox ID="UserScoreTextBox" runat="server" Text='<%# Bind("UserScore") %>' /> </td> </tr> </InsertItemTemplate> <ItemTemplate> <tr runat="server" id="trRow" style=""> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </ItemTemplate> <LayoutTemplate> <table runat="server"> <tr runat="server"> <td runat="server"> <table ID="itemPlaceholderContainer" runat="server" border="0" style=""> <tr runat="server" style=""> <th runat="server"> ID</th> <th runat="server"> UserName</th> <th runat="server"> UserPsw</th> <th runat="server"> UserPicturePath</th> <th runat="server"> UserScore</th> </tr> <tr ID="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style=""> </td> </tr> </table> </LayoutTemplate> <SelectedItemTemplate> <tr style=""> <td> <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /> </td> <td> <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' /> </td> <td> <asp:Label ID="UserPswLabel" runat="server" Text='<%# Eval("UserPsw") %>' /> </td> <td> <asp:Label ID="UserPicturePathLabel" runat="server" Text='<%# Eval("UserPicturePath") %>' /> </td> <td> <asp:Label ID="UserScoreLabel" runat="server" Text='<%# Eval("UserScore") %>' /> </td> </tr> </SelectedItemTemplate> </asp:ListView> </form> </body> </html>