gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:02:45 26 4
gpt4 key购买 nike

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

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

现在我想知道如何从 ItemDataBound 事件访问其中一列(如 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;
}

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

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

最佳答案

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

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

关于c# - 当数据源为 Linq 时访问 ItemDataBound 事件中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1470907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com