Iniettare record in DataList
Domanda
Vorrei suggerimenti su come iniettare un record nel mio DataList per dare un'opzione "Tutti". Ecco il mio codice, i dati provenienti dal database Northwind.
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatLayout="Flow" ShowFooter="False" ShowHeader="False"
RepeatDirection="Horizontal"
onitemcommand="DataList1_ItemCommand">
<ItemStyle CssClass="datalist" />
<ItemTemplate>
<%#(((DataListItem)Container).ItemIndex+1).ToString() %>
<asp:LinkButton ID="lbtnRegion" runat="server"
Text='<%# Eval("RegionDescription").ToString().Trim() %>'
CommandName='<%# DataBinder.Eval(Container.DataItem,"RegionID")%>' />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]"
ondatabinding="SqlDataSource1_Databinding"
onselected="SqlDataSource1_Selected">
</asp:SqlDataSource>
Sto usando il pulsante Collegamento nella Datalist per filtrare i territori e li visualizza in un GridView. Quello che vorrei fare è ad un certo nel processo di associazione dati, aggiungere un elemento nella DataList che fungerà da l'opzione ALL , qualche suggerimento sarebbe apprezzato.
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
LinkButton lbtn;
foreach (DataListItem dli in DataList1.Items)
{
lbtn = (LinkButton)dli.FindControl("lbtnRegion");
if (lbtn != null)
lbtn.ForeColor = System.Drawing.Color.White;
}
string command = e.CommandName;
lbtn = (LinkButton)e.Item.FindControl("lbtnRegion");
if (lbtn != null)
lbtn.ForeColor = System.Drawing.Color.YellowGreen;
DataView dv = GetData(ref command); // Pass the RegionId
gvTerritory.DataSource = dv;
gvTerritory.DataBind();
}
Grazie
Soluzione
Un modo è quello di un UNION ALL 'All' valore alla query recupero l'elenco dei discesa oggetti.
SELECT 'All', 'All Regions'
UNION ALL
SELECT [RegionID], [RegionDescription] FROM [Region]
Ma se avete un sacco di liste (o menu a discesa) come questo, è meglio la pratica per creare un controllo personalizzato che inietta un 'all' record per voi.
Altri suggerimenti
Ha funzionato con il seguente SQL:
SELECT '-1' AS 'RegionID', 'All Regions' AS 'RegionDescription'
UNION ALL SELECT [RegionID], [RegionDescription] FROM [Region]
Utilizzato questo su un menu a discesa, può funzionare lo stesso su un datalist
Protected Sub ddlDataSources_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDataSources.DataBound
ddlDataSources.Items.Insert(0, New ListItem("All Data Sources", 0))
End Sub