首页 新闻 会员 周边

强类型ASP.NET数据绑定的疑问

0
悬赏园豆:20 [已解决问题] 解决于 2011-12-09 10:11
Demo.cs
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class demo : System.Web.UI.Page
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 this.Repeater1.DataSource = this.GetList();
13 this.Repeater1.DataBind();
14 }
15
16 public IList<Student> GetList()
17 {
18 IList<Student> list = new List<Student>();
19 list.Add(new Student("张三", 22));
20 list.Add(new Student("李四", 25));
21 return list;
22 }
23 }
24
25
26 public class Student
27 {
28 public Student(string _Name, int _Age)
29 {
30 this.Name = _Name;
31 this.Age = _Age;
32 }
33 public string Name
34 {
35 set;get;
36 }
37 public int Age
38 {
39 set;get;
40 }
41 }
42
43 public static class Binder<TEntity> where TEntity : class
44 {
45 public static TResult Eval<TResult>(System.Web.UI.Page p, Func<TEntity, TResult> func)
46 {
47 return func((TEntity)p.GetDataItem());
48 }
49 }

 

Demo.aspx
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo.aspx.cs" Inherits="demo"%>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title></title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <asp:Repeater ID="Repeater1" runat="server">
12 <ItemTemplate>
13 姓名:<%#Binder<Student>.Eval(this,n=>n.Name) %>
14 年龄:<%#Binder<Student>.Eval(this,n=>n.Age) %><br />
15 </ItemTemplate>
16 </asp:Repeater>
17 </div>
18 </form>
19 </body>
20 </html>

上面是一个页面的代码,可以运行成功!

1、如何应用到多表查询时的强类型绑定?

2、如何应用到绑定的是DataTable数据源?

能解决DataTable那个多表查询的就解决了,如何解决,等待答案中……

 

看到博客http://kb.cnblogs.com/page/90080/留下的疑问

xu_happy_you的主页 xu_happy_you | 菜鸟二级 | 园豆:222
提问于:2011-12-04 22:33
< >
分享
最佳答案
0

你可以多表对应一个对象啊,

public class Student
27 {
28     public Student(string _Name, int _Age)
29     {
30         this.Name = _Name;
31         this.Age = _Age;
32     }
33     public string Name
34     {
35         set;get;
36     }
37     public int Age
38     {
39         set;get;
40     }
41 }

如果你有一个分数表,你可以把分数对象类当做属性写在Student中,再数据库的数据绑定在这个Student对象中就可以了。

收获园豆:10
悟行 | 专家六级 |园豆:12559 | 2011-12-05 11:02
其他回答(2)
0

你在Model里面添加一个类啊! 然后添加的view的时候直接选择者个类型就行了 啊

收获园豆:5
菜中菜 | 园豆:123 (初学一级) | 2011-12-04 22:46

如何应用到多表查询时的强类型绑定?

支持(0) 反对(0) xu_happy_you | 园豆:222 (菜鸟二级) | 2011-12-04 23:08

你在Model类创建一个类啊 把你要查询的属性  都添加进去啊  虽然这个方法比较笨  但还是可以解决问题啊

支持(0) 反对(0) 菜中菜 | 园豆:123 (初学一级) | 2011-12-05 21:20
0

是要把Ilist数据类型转换成DataTable吗?

收获园豆:5
静女 | 园豆:27 (初学一级) | 2011-12-06 20:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册