获取 DataList 中的行名称

发布于 2024-08-04 20:17:11 字数 197 浏览 8 评论 0原文

我有一个数据列表,想从表中提取行名称,我从中获取数据列表的值。这是我想做的一个例子。

<HeaderTemplate>
    'Get data row names
'Maybe something like Container.DataItem(row)?
    </HeaderTemplate>

I have a datalist and would like to pull the row names from the table that I am getting my values from for the datalist. Heres an example of what I would like to do.

<HeaderTemplate>
    'Get data row names
'Maybe something like Container.DataItem(row)?
    </HeaderTemplate>

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

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

发布评论

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

评论(2

白日梦 2024-08-11 20:17:11

如果您使用 DataTable 作为数据列表的数据源,则可以使用 OnItemCreated 方法并为 ItemCreated 事件提供自定义处理程序以添加列标题值。我不确定你为什么要这样做。听起来像 RepeaterGridView 可能更适合您的需求。无论如何,这是代码。

<asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated"
            ShowHeader="true" >
        <HeaderTemplate>
        </HeaderTemplate>
        </asp:DataList>

protected void Page_Load(object sender, EventArgs e)
    {  
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()))
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT [id], [name], [email], [street], [city] FROM [employee_tbl]", conn);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);
        }
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }

  protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            foreach (DataColumn col in dt.Columns)
            {
                Literal lit = new Literal();
                lit.Text = col.ColumnName;
                e.Item.Controls.Add(lit);
            }
        }
    }

If you are using a DataTable as a Data Source for your Data List you could use the OnItemCreated method and provide a custom handler for the ItemCreated event to add the column header values. I'm not sure why you would want to do this. It sounds like a Repeater or GridView might be better suited to your needs. At any rate, here's the code.

<asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated"
            ShowHeader="true" >
        <HeaderTemplate>
        </HeaderTemplate>
        </asp:DataList>

protected void Page_Load(object sender, EventArgs e)
    {  
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()))
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT [id], [name], [email], [street], [city] FROM [employee_tbl]", conn);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);
        }
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }

  protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            foreach (DataColumn col in dt.Columns)
            {
                Literal lit = new Literal();
                lit.Text = col.ColumnName;
                e.Item.Controls.Add(lit);
            }
        }
    }
人海汹涌 2024-08-11 20:17:11

您可以执行以下操作,但我怀疑它是否有效。我不相信创建标题时数据项可用。

((DataRowView)Container.DataItem).DataView.Table.Columns

如果这有效,您可以循环访问此集合并检查每个项目的 ColumnName 属性。

更好的想法是:

  1. 在代码隐藏中创建一个属性,返回适当列标题的 List。当您声明标头时,您可以在标记中引用此属性。
  2. 添加用于 ItemDataBound 事件和陷阱标头创建的处理程序。您仍然需要一种方法来引用数据元素,此时可能尚未准备好。

You could do the following, but I doubt it would work. I don't believe DataItems are available at the point when the Header is being created.

((DataRowView)Container.DataItem).DataView.Table.Columns

If this works, you can loop through this collection and inspect each item's ColumnName property.

A better idea would be to either:

  1. Create a property in codebehind that returns a List<string> of appropriate column headers. You can refer to this property in markup when you're declaring the header.
  2. Add a handler for the ItemDataBound event and trap header creation. You will still need a way to refer to the data elements, which probably haven't been prepped at this point.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文