C# 当数据源为 Linq 时访问 ItemDataBound 事件中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470907/
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 17:52:15  来源:igfitidea点击:

Access columns in ItemDataBound event when the datasource is Linq

c#asp.netlinq

提问by Fabian

Im setting the the datasource with the following code:

我使用以下代码设置数据源:

    protected void Page_Load(object sender, EventArgs e)
    {
        var vacancies = from v in db.Vacancies
                    join c in db.Customers on v.CustomerID equals c.CustomerID
                    join cp in db.CustomerPortals on c.CustomerID equals cp.CustomerID
                    where cp.PortalID == Master.Portal.ID
                    select new
                    {
                        Title = v.Title,
                        Internship = (v.ContractID == 6),
                        Hours = v.Hours,
                        City = v.Customer.City.Name,
                        Degree = v.Degree.Title,
                        Contract = v.Contract.Title,
                        CustomerID = v.CustomerID
                    };
        rVacancies.ItemDataBound += new RepeaterItemEventHandler(rVacancies_ItemDataBound);
        rVacancies.DataSource = vacancies;
        rVacancies.DataBind();
    }

Now i want to know how i can access 1 of the columns (like CustomerID) from the ItemDataBound event.

现在我想知道如何从 ItemDataBound 事件访问 1 个列(如 CustomerID)。

    void rVacancies_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
         // This doesnt seem to work, row would be null even though e.Item.DataItem has a value.
         DataRow row = (DataRow)e.Item.DataItem;
    }

I have figured out that e.Item.DataItem contains all the fields from my query and the type of e.Item.DataItem is

我发现 e.Item.DataItem 包含我查询中的所有字段,并且 e.Item.DataItem 的类型是

f__AnonymousType8<string,bool,byte,string,string,string,long>

采纳答案by Canavar

You're not binding a datarow to your control (you're binding an anonymous type), so you should not cast DataItem to DataRow.

您没有将数据行绑定到您的控件(您绑定的是匿名类型),因此您不应将 DataItem 强制转换为 DataRow。

Try to get your row's data as :

尝试将您行的数据设为:

var dataItem = e.Item.DataItem;
// For example get your CustomerID as you defined at your anonymous type :
string customerId = dataItem.CustomerID;

回答by Fabian

Finally found it, was as simple as the following:

终于找到了,就这么简单:

long customerID = long.Parse(DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString());

回答by RobD

This .Net 4.0 approach is also very cool indeed!

这种 .Net 4.0 方法也确实很酷!

public void PersonDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        dynamic person = e.Item.DataItem as dynamic;

        string name = person.Name;
        int age = person.Age;
    }
}

All Credit To: http://www.kristofclaes.be/blog/2010/08/12/anonymous-types-and-the-itemdatabound-event/

所有信用:http: //www.kristofclaes.be/blog/2010/08/12/anonymous-types-and-the-itemdatabound-event/

Because the page now shows an Error 404, here is the page from the Wayback Machine: Anonymous types and the ItemDataBound event (Archived version)

因为页面现在显示错误 404,这里是来自 Wayback Machine 的页面: 匿名类型和 ItemDataBound 事件(存档版本)