[英]Bind a Text Box to a Field Selected by SQLDataSource (VB.NET)
I'm looking for the easiest way to bind data from a SqlDataSource to textboxes dropped in Visual Studio 2008. 我正在寻找将数据从SqlDataSource绑定到Visual Studio 2008中放置的文本框的最简单方法。
For example, I have 4 textboxes currently that have Address, City, State, Zip. 例如,我目前有4个文本框,分别具有“地址”,“城市”,“州”,“邮政编码”。 I also have a SqlDataSource on the page fetching the ID of the record and selecting those 4 fields based on ID.
我在页面上还有一个SqlDataSource,它获取记录的ID并根据ID选择这4个字段。
How am I able to quickly bind each box to those particular fields selected? 如何快速将每个框绑定到所选的那些特定字段? I would think this would be really straight forward - but seems it's not.
我认为这确实很简单-但事实并非如此。 Seems like the answer is funneled towards having to create a GridView or some type of control.
答案似乎集中在必须创建GridView或某种类型的控件上。
Be gentle...I'm a nub :) 保持温柔...我是个小女孩:)
In general you are correct, if you want to use databinding you'll need to use an appropriate control. 通常,您是对的,如果要使用数据绑定,则需要使用适当的控件。 For this example I'd suggest using a FormView - it is designed to display the results from a single database record and uses templates, meaning you'll have complete control over the output.
对于此示例,我建议使用FormView-它旨在显示单个数据库记录的结果并使用模板,这意味着您将完全控制输出。 This article is probably a good place to start: FormView Control: Step by Step .
本文可能是开始的好地方: FormView控件:逐步 。
To read the values bound to the FormView in the code-behind class you would need to create an event handler for the FormView's DataBound
event. 要在代码隐藏类中读取绑定到FormView的值,您需要为FormView的
DataBound
事件创建一个事件处理程序。 In that event handler you would reference the controls programmatically via FindControl
, like so: 在该事件处理程序中,您将通过
FindControl
以编程方式引用控件,如下所示:
Dim myLabel As Label = CType(FormViewID.FindControl("id"), Label)
Here, id would be the ID
of the Label whose value you were interested in. Once you have a reference to the Label you can get its value using myLabel.Text
. 在这里, id将是您对其值感兴趣的Label的
ID
。一旦获得对Label的引用,就可以使用myLabel.Text
获得其值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.