无法在嵌入式 ASPX 代码中引用动态对象
我正在 Page_Init 事件期间创建一个 List 成员变量。我在从 *.aspx 页面中嵌入的 C# 代码引用列表中的对象时遇到问题。该错误是运行时绑定程序异常,显示“'object' 不包含 'JobID' 的定义”。
当调用调试器时,我可以看到 foreach 循环的变量 j 确实有一个名为 JobID 的动态属性,并且它填充了一个 int 值。所以,我的问题是为什么我的嵌入式 C# 代码无法使用动态对象。是否有<%@导入%>?我需要使用动态对象的声明?我尝试添加 <%@ Import namespace="System.Dynamic" %>但这没有帮助。
感谢您的帮助。 标记
隐藏代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Jobbarama.WebCode;
using DataModel;
public partial class contact : System.Web.UI.Page
{
public List<dynamic> JobList { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
SessionManager mgr = SessionManager.Current;
using (myEntities context = new myEntities())
{
var qry = from c in context.vjobList
where c.CampaignID == mgr.CampaignID
select new
{
c.JobID, c.JobTitle, c.CompanyName, c.InterestDate, c.InterestLevel
};
JobList = qry.ToList<dynamic>();
}
}
}
}
ASPX 代码:
<select id='cboJob' name='cboJob' style='width: 150px;'>
<%foreach (var j in JobList){ %>
<option value="<%=j.JobID %>"><%=j.JobTitle%> [<%=j.CompanyName%>]</option>
<%} %>
</select>
I'm creating a List member variable during my Page_Init event. I'm having a problem referencing the objects in the list from my embedded C# code in the *.aspx page. The error is a Runtime Binder Exception that says "'object' does not contain a definition for 'JobID'".
When the debugger is invoked, I can see that the foreach loop's variable j does indeed have a dynamic property named JobID and it's filled with an int value. So, my question is why my embedded C# code can't work with the dynamic object. Is there an <%@ Import %> statement that I need to work with dynamic objects? I tried adding <%@ Import namespace="System.Dynamic" %> but that didn't help.
Thanks for the help.
Mark
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using Jobbarama.WebCode;
using DataModel;
public partial class contact : System.Web.UI.Page
{
public List<dynamic> JobList { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
SessionManager mgr = SessionManager.Current;
using (myEntities context = new myEntities())
{
var qry = from c in context.vjobList
where c.CampaignID == mgr.CampaignID
select new
{
c.JobID, c.JobTitle, c.CompanyName, c.InterestDate, c.InterestLevel
};
JobList = qry.ToList<dynamic>();
}
}
}
}
ASPX Code:
<select id='cboJob' name='cboJob' style='width: 150px;'>
<%foreach (var j in JobList){ %>
<option value="<%=j.JobID %>"><%=j.JobTitle%> [<%=j.CompanyName%>]</option>
<%} %>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜这可能是由于在不同程序集中使用匿名类和 aspx 后期编译内容而导致的权限问题。
您可以使用impromptu-interface来实现此目的。
然后你创建一个接口(我使用动态,因为我不知道你的类型)
你的属性应该使用该接口
并创建你的列表只需添加
.AllActLike()
这应该工作,因为它生成一个轻量级 dlr 代理并将上下文设置为其自身的匿名类,因此它认为它始终具有访问权限,这与使用动态关键字调用不同。
My guess this might be a permission issue due to using an anonymous class and aspx late compiling stuff in different assemblies.
You can use impromptu-interface to make this work.
then you make an Interface (I'm using dynamic because i don't know your types)
Your Property should use the interface
And to create your list just add
.AllActLike<ISelectJob>()
And this should work, as it generates a lightweight dlr proxy and sets the context to the anonymous class its self so it thinks it always has access, unlike calling with the dynamic keyword.
使用 LinqDataSource、设置 OnSelecting 命令并使用转发器或数据列表进行显示怎么样?
What about using a LinqDataSource, setting the OnSelecting command, and using a repeater or datalist to display?