将数据绑定到ListView ItemDataBound内的Gridview实例

本文关键字:内的 Gridview 实例 ItemDataBound ListView 数据绑定 | 更新日期: 2023-09-27 18:11:24

我得到了ListView控件,该控件的ItemTemplate 中有一个Gridview

<asp:ListView ID="InboxList" runat="server" OnItemDataBound="InboxList_ItemDataBound" OnItemCreated="InboxList_ItemCreated">
   <LayoutTemplate>
      <tr runat="server" id="itemPlaceholder"></tr>
   </LayoutTemplate>
   <ItemTemplate>
      <tr>
         <td>
            <asp:Label Visible="false" ID="Id" runat="server" Text='<%#Eval("Id") %>' />
         </td>
      </tr>
      <tr>
         <td>
            <asp:GridView ID="itemsGridView" runat="server" AutoGenerateColumns="False" BorderWidth="0px" ShowFooter="true" ShowHeader="true" HeaderStyle-CssClass="internal" OnRowDataBound="itemsGridView_RowDataBound">
               <Columns>
                  <asp:TemplateField HeaderText="Declared Value">
                     <ItemTemplate>
                        <asp:Label ID="declaredValue" runat="server" ><%# Eval("DECLAREDVALUE") %></asp:Label>
                     </ItemTemplate>
                     <FooterTemplate>
                        <asp:Label ID="anotherValue" runat="server"></asp:Label>
                     </FooterTemplate>
                  </asp:TemplateField> 
               </Columns>
            </asp:GridView>
         </td>
      </tr>
   </ItemTemplate>

我正在尝试访问GridviewRowDataBound事件中具有ID="declaredValue"值的此标签,该事件在ListView ItemDataBound激发时为激发。。

Listview ItemDatabound事件

protected void InboxList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem ||e.Item.ItemType == ListViewItemType.InsertItem||e.Item.ItemType == ListViewItemType.EmptyItem)
    {
        DataTable dt = new DataTable();
        DAL.GetDataID(int.Parse(((Label)e.Item.FindControl("Id")).Text), dt);
        ((GridView)(e.Item.FindControl("itemsGridView"))).DataSource = dt;
        ((GridView)(e.Item.FindControl("itemsGridView"))).DataBind();
    }
}

GridView行数据绑定事件

protected void itemsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lbl = (Label)e.Row.FindControl("declaredValue");
        if (lbl.Text != "")
        {
            //Here Text value equals "" always!
            string value = lbl.Text;
        }
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
    }
}

我可以访问这个标签,但每次它都是Text="",但它有价值,我不知道什么时候是获取其价值的最佳时机。

请告知。谢谢

将数据绑定到ListView ItemDataBound内的Gridview实例

您在declaredValueText属性中有空string,因为您将GridView与空DataTable绑定,您需要在其中添加一些数据。

if (e.Item.ItemType == ListViewItemType.DataItem ||e.Item.ItemType == ListViewItemType.InsertItem||e.Item.ItemType == ListViewItemType.EmptyItem)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("DECLAREDVALUE");
    DataRow dr = dt.NewRow();
    dr["DECLAREDVALUE"] = "hello";
    dt.Rows.Add(dr);
    ((GridView)(e.Item.FindControl("itemsGridView"))).DataSource = dt;
    ((GridView)(e.Item.FindControl("itemsGridView"))).DataBind();
}

编辑OP更改了问题并添加了将数据加载到数据表中的代码。

您需要e.Row.DataItem来查找分配给每个行标签的值。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    Label lbl = (Label)e.Row.FindControl("declaredValue");
    string valueAssignedToLabel = DataBinder.Eval(e.Row.DataItem, "declaredValueColumnOfDataTable").ToString();
    if (valueAssignedToLabel  != "")
    {
        //Here Text value equals "" always!
        string value = lbl.Text;
    }
}