问题描述
我在我的Page_Init事件创建一个列表成员变量。我有一个问题,从我的嵌入式C#code在* .aspx页面中引用该列表中的对象。错误是运行时绑定的异常,说:'对象'不包含'作业ID'的定义。
在被调用调试器,我可以看到,foreach循环的变量j确实有一个名为作业ID动态属性和它充满了一个int值。所以,我的问题是,为什么我的嵌入式C#code不能与动态对象的工作。是否有一个<%@导入%>语句,我需要用动态对象的工作?我尝试添加<%@导入命名空间=System.Dynamic%>但是这并没有帮助
。感谢您的帮助。马克
code背后:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用Jobbarama.Web code;
使用的DataModel;
公共部分类接触:System.Web.UI.Page
{
公开名单<动态>招贤纳才{获得;组; }
保护无效Page_Init(对象发件人,EventArgs的)
{
是SessionManager经理= SessionManager.Current;
使用(myEntities上下文=新myEntities())
{
VAR QRY =从C在context.vjobList
其中,c.CampaignID == mgr.CampaignID
选择新
{
c.JobID,c.JobTitle,c.CompanyName,c.InterestDate,c.InterestLevel
};
作业表= qry.ToList<动态>();
}
}
}
}
ASPX code:
<选择一个id ='cboJob的名字='cboJob'风格='宽度:150像素;'>
<%的foreach(在招贤纳才变种j)条{%GT;
<期权价值=<%= j.JobID%>><%= j.JobTitle%>并[d(%)= j.CompanyName%GT;]其中; /选项>
<%}%GT;
< /选择>
怎么样使用的LinqDataSource,设置OnSelecting命令,并使用一个中继器或DataList控件来显示?
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>
What about using a LinqDataSource, setting the OnSelecting command, and using a repeater or datalist to display?
这篇关于不能引用在嵌入式ASPX code动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!