如何手动将复杂对象数据绑定到模板化控件(例如网格视图中的行)?
我在这里正在努力解决数据绑定语法。 例如,我有一个像这样的数据结构 -
public class Course{
public string CourseName {get;set;}
public string CourseCode {get;set;}
public List<Instructor> InstructorsTeaching{get;set;}
}
public class Instructor{
public string InstructorName{get;set;}
public string InstructorCode{get;set;}
}
现在,如果我想手动绑定此列表课程以说出网格视图,我可以
<asp:TextBox runat="server" ID="tbCourseName" Text='<%# Bind("CourseName")%>'/>
在指定网格的编辑模板时执行此操作,但如何绑定讲师教学属性以说出列表框同一行,我无法弄清楚语法,这是我尝试过但失败的示例
<asp:ListBox runat="server" ID="tbInstructors"
DataSource='<%# Eval("InstructorsTeaching") as List<Instructor> %>'>
<asp:ListItem Text='<%# Bind("InstructorCode")%>'
Value='<%# Bind("InstructorName")%>'/>...
<as:ListBox/>
我上面的代码肯定不起作用:)。 理想情况下,我想在标记中而不是在代码后面执行此操作。
I am struggling with the databinding syntax here. For example I have a data structure like this -
public class Course{
public string CourseName {get;set;}
public string CourseCode {get;set;}
public List<Instructor> InstructorsTeaching{get;set;}
}
public class Instructor{
public string InstructorName{get;set;}
public string InstructorCode{get;set;}
}
Now if I want to bind this List Courses to say a gridview manually, I could do
<asp:TextBox runat="server" ID="tbCourseName" Text='<%# Bind("CourseName")%>'/>
while specifying for edit template of the grid but how do I bind the Instructors teaching property to say a ListBox in the same row, I cant figure out the syntax , here is an exaple of what I tried and failed
<asp:ListBox runat="server" ID="tbInstructors"
DataSource='<%# Eval("InstructorsTeaching") as List<Instructor> %>'>
<asp:ListItem Text='<%# Bind("InstructorCode")%>'
Value='<%# Bind("InstructorName")%>'/>...
<as:ListBox/>
My above code does not work for sure :). Ideally I would like to do this in markup instead of code behind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经发现了 ASP.NET 2 路数据绑定糟糕的一个主要原因:您确实无法进行嵌套的 2 路数据绑定。
一方面,虽然您可以使用 Eval 来完成此操作,但 ASP.NET 不允许使用 Bind 表达式使用嵌套图形语法(即 <%# Bind("Customer.FirstName") %>)。
除此之外,对于像您的场景这样的嵌套列表控件,每个列表都需要一个额外的 DataSource 控件。 您正在 ListBox 上设置 DataSource,这适用于 Eval 表达式,但要使绑定表达式起作用,您必须使用 DataSourceID 来提供提供内部结果集的 DataSource 控件的 ID。 即使这样,你的结果也会很混乱,因为你一次只能更新一个数据源。
2 路数据绑定可能是在考虑 SqlDataSource 而不是 ObjectDataSource 的情况下编写的。 如果您有一个多级对象图,您会发现使用 2 路数据绑定很痛苦。
You've hit on a major reason that ASP.NET 2-way databinding sucks: You really can't do nested 2-way data-binding.
For one thing, although you can do it with Eval, ASP.NET doesn't allow nested graph syntax with the Bind expression, (i.e. <%# Bind("Customer.FirstName") %>).
Beyond that, for nested list controls like your scenario, each list will require an additional DataSource control. You are setting the DataSource on your ListBox, which will work for Eval expressions, but for binding expressions to work, you must use DataSourceID to provide the ID of a DataSource control that provides the inner result set. And even then your results would be kludgy since you could only update one datasource at a time.
2-way databinding was probably written with SqlDataSource in mind and not ObjectDataSource. If you have a multi-level object graph, you'll find it painful to use 2-way databinding.
我认为您无法设置这样的数据源,请尝试在 GridView的RowDataBound事件
I don't think you can set a datasource like that, try setting it on GridView's RowDataBound event