Bind data to Gridview instance inside ListView ItemDataBound

孤者浪人 提交于 2019-12-12 18:51:14

问题


I got ListView control that has a Gridview inside its ItemTemplate

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

I'm trying to access this Label with ID="declaredValue" value in the RowDataBound Event of my Gridview which is Fires when ListView ItemDataBound Fire..

Listview ItemDatabound Event

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

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)
    {
    }
}

I got access to this label but every time it's Text="" But it has value, I don't know what's the best time to access its value.

Please advise. Thanks.


回答1:


You have empty string in Text property of declaredValue because you are binding the GridView with empty DataTable you need to add some data in it.

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

Edit OP changed the question and added code for loading data into data table.

You need to e.Row.DataItem to find the value being assigned to each row label.

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


来源:https://stackoverflow.com/questions/16277772/bind-data-to-gridview-instance-inside-listview-itemdatabound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!