定到ListView控件的ItemDataBound里面的Gri

定到ListView控件的ItemDataBound里面的Gri

本文介绍了数据绑定到ListView控件的ItemDataBound里面的GridView实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的ListView 具有 GridView控件里面的的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>

我试图用 ID =declaredValue访问此标签中的的RowDataBound 事件我的价值的GridView 这是时触发的ListView的ItemDataBound 消防..

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

列表视图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的RowDataBound事件

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 everytime its Text="" But it has value,i donno what's the best time to access its value~

请指教。
谢谢你。

Please advise.Thanks.

推荐答案

您已经清空字符串文本属性 declaredValue 的,因为你要绑定的 GridView控件数据表你需要在它添加一些数据。

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

修改 OP改变的问题,并添加加载数据code到数据表。

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

您需要 e.Row.DataItem 找到该值被分配给每个行标签。

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

这篇关于数据绑定到ListView控件的ItemDataBound里面的GridView实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:22