|
Il presente articolo è stato tradotto manualmente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale.
|
Traduzione
Originale
|
Cenni preliminari sul controllo server Web ListView
Utilizzando la proprietà DataSourceID, che consente di associare il controllo ListView a un controllo origine dati, ad esempio il controllo SqlDataSource. Si consiglia questo approccio perché consente al controllo ListView di sfruttare le funzionalità del controllo origine dati. Fornisce inoltre la funzionalità incorporata per l'ordinamento, lo spostamento, l'inserimento, l'eliminazione e l'aggiornamento. Questo approccio consente inoltre di utilizzare le espressioni di associazione bidirezionali. Per ulteriori informazioni sui controlli origine dati, vedere Cenni preliminari sui controlli origine dati. Utilizzando la proprietà DataSource, che consente di eseguire l'associazione a vari oggetti, tra cui i DataSet ADO.NET, i lettori dati e le strutture in memoria quali gli insiemi. Questa opzione richiede la scrittura di codice per qualsiasi funzionalità aggiuntiva, ad esempio l'ordinamento, lo spostamento e l'aggiornamento.
Raggruppamento di elementi
Nota |
|---|
Modelli disponibili
Creazione di un modello di elementi
<asp:ListView runat="server" ID="ListView1"
DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table runat="server" id="table1" >
<tr runat="server" id="itemPlaceholder" ></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td runat="server">
<%-- Data-bound content. --%>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:ListView runat="server" ID="ListView1"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr runat="server">
<td runat="server">
<%-- Data-bound content. --%>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Nota |
|---|

<asp:ListView runat="server" ID="EmployeesListView"
DataSourceID="EmployeesDataSource"
DataKeyNames="EmployeeID">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblEmployees"
style="width:460px">
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<asp:DataPager runat="server" ID="DataPager" PageSize="3">
<Fields>
<asp:NumericPagerField
ButtonCount="5"
PreviousPageText="<--"
NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top" colspan="2" align="center"
class="EmployeeName">
<asp:Label ID="FirstNameLabel" runat="server"
Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server"
Text='<%#Eval("LastName") %>' />
</td>
</tr>
<tr style="height:72px" runat="server">
<td valign="top" class="EmployeeInfo">
<asp:Label ID="JobTitleLabel" runat="server"
Text='<%#Eval("JobTitle") %>' />
<br />
<asp:HyperLink ID="EmailAddressLink" runat="server"
Text='<%#Eval("EmailAddress") %>'
NavigateUrl='<%#"mailto:" + Eval("EmailAddress") %>' />
<br />
<asp:Label ID="PhoneLabel" runat="server"
Text='<%#Eval("Phone") %>' />
</td>
<td valign="top" class="EmployeeAddress">
<asp:Label ID="AddressLine1Label" runat="server"
Text='<%#Eval("AddressLine1") %>' />
<br />
<asp:Panel ID="AddressLine2Panel" runat="server"
Visible='<%#Eval("AddressLine2").ToString() != String.Empty %>'>
<asp:Label ID="AddressLine2Label" runat="server"
Text='<%#Eval("AddressLine2").ToString()%>' />
<br />
</asp:Panel>
<asp:Label ID="CityLabel" runat="server"
Text='<%#Eval("City") %>' />,
<asp:Label ID="StateProvinceNameLabel" runat="server"
Text='<%#Eval("StateProvinceName") %>' />
<asp:Label ID="PostalCodeLabel" runat="server"
Text='<%#Eval("PostalCode") %>' />
<br />
<asp:Label ID="CountryRegionNameLabel" runat="server"
Text='<%#Eval("CountryRegionName") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Creazione di un modello di gruppo
<asp:ListView runat="server" ID="ListView1"
DataSourceID="SqlDataSource1"
GroupItemCount="5">
<LayoutTemplate>
<table runat="server" id="table1">
<tr runat="server" id="groupPlaceholder">
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr runat="server" id="tableRow">
<td runat="server" id="itemPlaceholder" />
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server">
<%-- Data-bound content. --%>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' />
</td>
</ItemTemplate>
</asp:ListView>
Nota |
|---|

<asp:ListView runat="server" ID="ProductsListView"
GroupItemCount="3"
DataSourceID="ProductsSqlDataSource" DataKeyNames="ProductID">
<LayoutTemplate>
<table cellpadding="2" runat="server"
id="tblProducts" style="height:320px">
<tr runat="server" id="groupPlaceholder">
</tr>
</table>
<asp:DataPager runat="server" ID="DataPager"
PageSize="9">
<Fields>
<asp:NumericPagerField ButtonCount="3"
PreviousPageText="<--"
NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<GroupTemplate>
<tr runat="server" id="productRow"
style="height:80px">
<td runat="server" id="itemPlaceholder">
</td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td valign="top" align="center" style="width:100" runat="server">
<asp:Image ID="ProductImage" runat="server"
ImageUrl='<%#"~/images/thumbnails/" +
Eval("ThumbnailPhotoFileName") %>'
Height="49" /><br />
<asp:HyperLink ID="ProductLink" runat="server"
Target="_blank" Text='<% #Eval("Name")%>'
NavigateUrl='<%#"ShowProduct.aspx?ProductID=" +
Eval("ProductID") %>' />
</td>
</ItemTemplate>
</asp:ListView>
<asp:ListView runat="server" ID="ListView1"
DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table runat="server" id=" table1">
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<asp:DataPager runat="server" ID="DataPager" PageSize="5">
<Fields>
<asp:NumericPagerField ButtonCount="10"
PreviousPageText="<--"
NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<%-- Data-bound content. --%>
</tr>
</ItemTemplate>
</asp:ListView>

<asp:DataPager runat="server" ID="DataPager" PageSize="10">
<Fields>
<asp:NumericPagerField ButtonCount="10"
CurrentPageLabelCssClass="CurrentPage"
NumericButtonCssClass="PageNumbers"
NextPreviousButtonCssClass="PageNumbers" NextPageText=" > "
PreviousPageText=" < " />
</Fields>
</asp:DataPager>

<asp:DataPager runat="server" ID="EmployeesDataPager" PageSize="8">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<asp:TextBox ID="CurrentRowTextBox" runat="server"
AutoPostBack="true"
Text="<%# Container.StartRowIndex + 1%>"
Columns="1"
style="text-align:right"
OnTextChanged="CurrentRowTextBox_OnTextChanged" />
to
<asp:Label ID="PageSizeLabel" runat="server" Font-Bold="true"
Text="<%# Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize %>" />
of
<asp:Label ID="TotalRowsLabel" runat="server" Font-Bold="true"
Text="<%# Container.TotalRowCount %>" />
</PagerTemplate>
</asp:TemplatePagerField>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
protected void CurrentRowTextBox_OnTextChanged(object sender, EventArgs e) { TextBox t = (TextBox)sender; DataPager pager = (DataPager)EmployeesListView.FindControl("EmployeesDataPager"); pager.SetPageProperties(Convert.ToInt32(t.Text) - 1, pager.PageSize, true); }
<asp:ListView runat="server" ID="ListView1" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<asp:LinkButton runat="server" ID="SortButton"
Text="Sort" CommandName="Sort" CommandArgument="LastName" />
<table runat="server" id="table1">
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
<asp:DataPager runat="server" ID="DataPager" PageSize="20">
<Fields>
<asp:NumericPagerField ButtonCount="10"
PreviousPageText="<--"
NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td><asp:Label runat="server" ID="FirstNameLabel"
Text='<%# Eval("FirstName")' /></td>
<td><asp:Label runat="server" ID="LastNameLabel"
Text='<%# Eval("LastName")' /></td>
</tr>
</ItemTemplate>
</asp:ListView>
Impostazione dinamica dell'espressione di ordinamento
protected void EmployeesListView_OnSorting(object sender, ListViewSortEventArgs e) { if (String.IsNullOrEmpty(e.SortExpression)) { return; } string direction = ""; if (ViewState["SortDirection"] != null) direction = ViewState["SortDirection"].ToString(); if (direction == "ASC") direction = "DESC"; else direction = "ASC"; ViewState["SortDirection"] = direction; string[] sortColumns = e.SortExpression.Split(','); string sortExpression = sortColumns[0] + " " + direction; for (int i = 1; i < sortColumns.Length; i++) sortExpression += ", " + sortColumns[i] + " " + direction; e.SortExpression = sortExpression; }
Una proprietà NumericButtonCssClass che consente di specificare il nome della classe CSS per i pulsanti che consentono lo spostamento tra le pagine di dati in base al numero. Una proprietà CurrentPageLabelCssClass che consente di specificare il nome della classe CSS per il numero di pagina corrente. Una proprietà NextPreviousButtonCssClass che consente di specificare il nome della classe CSS per i pulsanti che consentono lo spostamento al gruppo precedente o successivo di pulsanti numerici.
Nota