惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - ji yang

页面点击分析工具设计与实现 使用FreeMind代替PowerPoint做演示 Velocity 2011大会归来 通过扩展属性为SqlServer的索引添加注释信息 memcached对中文key的支持 更安全的服务器:Windows账号权限修改监控 网站活跃用户统计的思路与设计 以服务的方式提供站点基础功能支持 safari的url地址转义问题 fastdfs dotnet 客户端发布 go lang web项目的部署与发布 从设计图到HTML页面时的注意点 关于加班 关于Google App Engine SOA中国路线图活动感受 文章被转载了 Asp.net中基类页的设计和使用 产品营销上的求同策略
一个简单的Spring.net绑定例子
ji yang · 2007-10-19 · via 博客园 - ji yang

下午的时候碰到一个需求,同事需要根据数据库里的表,自动在Aspx Page上添加相应的TextBox控件,并将值绑定到TextBox文本框上,同样的,在保存时也希望能保存回数据库。

为了支持双向绑定,用Spring.net的

Spring.DataBinding.BaseBindingManager来做是个简单的方法,碰到的问题是:Spring.net的绑定表达式是基于属性的,而动态加入的txtName, txtAge是无法通过"Page.txtName", "Page.txtAge"来访问的,即我们动态加入的控件被放到了Page.Form.Controls里了。

解决的办法很简单,为页面加入this["name"]方式的属性访问器:

 public Control this[string name]
 {
  get { return this.FindControl(name); } 
  }

在绑定表达式只要这样写即可:

['name'].Text ,由此把动态加入的控件看着Page的this["name"]属性。

完整的例子代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using Spring.DataBinding;public class People
{
    
string name;
    
public string Name
    {
        
get { return name; }
        
set { name = value; }
    }
}
public partial class PollutionManage_Spring : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            BaseBindingManager manager 
= new BaseBindingManager();

            TextBox box 

= new TextBox();
            box.Text 
= "hello";
            box.ID 
= "hi";
            
this.Form.Controls.Add(box);

            People p 

= new People();
            p.Name 
= "jiyang";

            manager.AddBinding(

"Name""['hi'].Text");

            manager.BindSourceToTarget(p, 

thisnull);
        }
    }
public Control this[string name] 
    { 
        
get { return this.FindControl(name); } 
    }
}