在ItemDataBound事件访问列时的数据源是Linq的 [英] Access columns in ItemDataBound event when the datasource is Linq

查看:274
本文介绍了在ItemDataBound事件访问列时的数据源是Linq的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林设置数据源用下面的代码:

 保护无效的Page_Load(对象发件人,EventArgs五)
{
VAR空缺=从v在db.Vacancies
将C在db.Customers上v.CustomerID等于c.CustomerID
在db.CustomerPortals加入CP上c.CustomerID等于CP。客户ID
,其中cp.PortalID == Master.Portal.ID
选择新的
{
标题= v.Title,
=实习(v.ContractID == 6 ),
使用时间为v.Hours,
市= v.Customer.City.Name,
级= v.Degree.Title,
合同= v.Contract.Title,
=客户ID v.CustomerID
};
rVacancies.ItemDataBound + =新RepeaterItemEventHandler(rVacancies_ItemDataBound);
rVacancies.DataSource =空缺;
rVacancies.DataBind();
}

现在我想知道我可以访问列1(如客户ID )从ItemDataBound事件。

 无效rVacancies_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
//这似乎没有工作,行会,即使e.Item.DataItem有一个null值。
的DataRow行=(DataRow中)e.Item.DataItem;
}



我已经想通了,e.Item.DataItem包含的所有字段我查询和e.Item.DataItem的类型是

  f__AnonymousType8<字符串,布尔,字节,字符串,字符串,字符串长> ; 


解决方案

你不是一个数据行绑定到你的控制(你要绑定一个匿名类型),所以你不应该投的DataItem到的DataRow



尝试让你行的数据为:

  VAR的DataItem = e.Item.DataItem; 
//例如让您的客户ID,你在你的匿名类型定义:
字符串客户ID = dataItem.CustomerID;


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.

    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

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

解决方案

You're not binding a datarow to your control (you're binding an anonymous type), so you should not cast DataItem to 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;

这篇关于在ItemDataBound事件访问列时的数据源是Linq的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆