从ListView ItemDataBound获取数据值

时间:2009-10-19 12:46:08

标签: c# asp.net

我确定我以前做过这件事,但真的不记得。

在ListView的ItemDataBound事件中,我需要获取实际的数据值。我似乎无法在传入的ListViewItemEventArgs对象中找到它。

由于

4 个答案:

答案 0 :(得分:15)

使用ListViewDataItem事件中的ItemDataBound

protected void yourListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        YourDataSource yourDataSource= (YourDataSource )dataItem.DataItem;            

    }

}

答案 1 :(得分:8)

我认为你所追求的是ListViewDataItem.DataItem

答案 2 :(得分:3)

protected void Score_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
{
    if (e.Item is RadListViewItem)
    {
        RadListViewDataItem item = e.Item as RadListViewDataItem;
        object dataItem = ((System.Data.DataRowView)(((RadListViewDataItem)e.Item).DataItem)).Row.ItemArray[2].ToString();
        string raetest = Convert.ToString(dataItem);
    }
}

答案 3 :(得分:-2)

<asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        ConvertEmptyStringToNull="true"        
        OnItemDataBound="ContactsListView_ItemDataBound"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr style="background-color: #ADD8E6" runat="server">
                <th runat="server">First Name</th>
                <th runat="server">Last Name</th>
                <th runat="server">E-mail Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr style="background-color: #CAEEFF" runat="server">
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>

服务器端

protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
      {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
          // Display the e-mail address in italics.
          Label EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
          // EmailAddressLabel.Font.Italic = true;
          string valueoftheControl = EmailAddressLabel.Text;  
          /* you have to get the value like this. 
             If its a dropdown or any other use their 
             corresponding property to get the value.*/
        }
      }