C# 从 ListView ItemDataBound 获取数据值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1588500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:06:22  来源:igfitidea点击:

Get Data Value From ListView ItemDataBound

c#asp.net

提问by Gavin

I'm sure I've done this before but really cant remember how.

我确定我以前做过这个,但真的不记得是怎么做的。

In the ItemDataBound event of a ListView I need to get the actual data value. I cant seem to find it in the ListViewItemEventArgs object that gets passed in.

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

Thanks

谢谢

采纳答案by stuartd

I think what you're after is the ListViewDataItem.DataItem

我想你所追求的是ListViewDataItem.DataItem

回答by solairaja

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

Server side

服务器端

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.*/
        }
      }

回答by rick schott

Use the ListViewDataItemin the ItemDataBoundevent:

ItemDataBound事件中使用ListViewDataItem

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

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

    }

}

回答by El Mas

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