数据绑定用户控件 [英] Databinding a user control

查看:92
本文介绍了数据绑定用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用户控件,并且我想绑定一个文本框(asp:TextBox)以绑定到数据库字段.计划在两种表单中使用用户控件,并且每种表单都有用户控件要绑定到的不同表(数据).

快速阅读告诉我:不幸的是,这些控件与基础数据源紧密相关.控件,该控件必须基于IEnumerable.因此,例如,不支持绑定到单个实体对象."我正在运行在WinXP SP3主机和IIS 5.1上运行的ASP.NET/VS 2008/.NET 3.5(+ SP 1),以防万一.
源代码此处.

:confused:

解决方案

如果您只有一个文本框,那么您实际上并不会像gridview那样将数据绑定到源.当然,您可以仅添加一个属性,该属性是一个存储的proc的名称,该属性返回单个文本项,这就是您的文本框显示的内容吗?

当然,当代码使用控件时,最好调用正确的数据方法,否则在表示层中将包含SQL,否则控件将与数据层紧密绑定.

I have created a user control, and, I would like to bind a textbox (asp:TextBox) to be bound to a database field. The user control is planned to be used within two forms, and, each form has a different table (data) to which the user control is to be bound.

A quick read tells me that: "Unfortunately, these controls are closely tied to an underlying data source control, which must be IEnumerable-based. So binding to an individual entity object, for example, is not supported." I am using ASP.NET / VS 2008 / .NET 3.5 (+SP 1) running on a WinXP SP3 host, and, IIS 5.1, just in case it''s relevant.

Source code here.

:confused:

解决方案

If you have a single textbox, then you are not really databinding to a source the way a gridview works. Surely you can just add a property that is the name of a stored proc that returns a single item of text, which then is what your textbox shows ?

Of course, it would be better for your code to call the right data methods when it uses the control, otherwise you''ll have SQL in your presentation layer, or your control will be too tightly bound to your data layer.


这篇关于数据绑定用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆