0

我想要关于如何将记录注入我的 DataList 以提供“全部”选项的建议。这是我的代码,数据来自 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>

我正在使用 Datalist 中的链接按钮来过滤区域并将它们显示在 GridView 中。 我想做的是在数据绑定过程中的某些地方,在 DataList 中添加一个将充当 ALL 选项的项目,任何建议都将不胜感激。

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();
}

谢谢

4

3 回答 3

0

一种方法是将“全部”值 UNION ALL 到获取下拉项目列表的查询中。

SELECT 'All', 'All Regions' 
UNION ALL 
SELECT [RegionID], [RegionDescription] FROM [Region]

但是,如果您有很多这样的列表(或下拉列表),最好创建一个自定义控件来为您注入“全部”记录。

于 2009-02-03T00:07:24.450 回答
0

它使用以下 SQL:

SELECT '-1' AS 'RegionID', 'All Regions' AS 'RegionDescription' 
UNION ALL SELECT [RegionID], [RegionDescription] FROM [Region]
于 2009-02-03T00:16:29.657 回答
0

在下拉列表中使用它,可能在数据列表上工作相同

  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
于 2009-11-25T15:20:36.150 回答