将记录注入DataList

发布于 2024-07-13 00:46:46 字数 1891 浏览 9 评论 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>

我使用数据列表中的“链接”按钮来过滤区域并将其显示在 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();
}

谢谢

I would like suggestions on how to inject a record into my DataList to give an "All" option. Here is my code, data coming from the Northwind database.

<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>

I am using the Link button in the Datalist to filter the territories and display them in a GridView.
What I would like to do is at some in the databinding process, add an Item in the DataList that will act as the ALL option, any suggestions would be appreciated.

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡莣 2024-07-20 00:46:46

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

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

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

One way is to UNION ALL a 'All' value to the query fetching the list of drop down items.

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

But if you have a lot of lists (or dropdowns) like this, it is better practice to create a custom control that injects an 'All' record for you.

稍尽春風 2024-07-20 00:46:46

它与以下 SQL 一起使用:

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

It worked with the following SQL:

SELECT '-1' AS 'RegionID', 'All Regions' AS 'RegionDescription' 
UNION ALL SELECT [RegionID], [RegionDescription] FROM [Region]
温折酒 2024-07-20 00:46:46

在下拉列表中使用它,在数据列表上可能会起到相同的作用

  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

Used this on a drop down, may work the same on a 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文