简体   繁体   English

将文本框绑定到SQLDataSource(VB.NET)选择的字段

[英]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.

相关问题 VB.NET列表框选定的索引 - VB.NET List Box Selected Index 从VB.NET中的会话填充SQLDataSource - Populating SQLDataSource from Sessions in VB.NET 使用ASP.NET(VB.NET)将文本框字段与服务器端数据库进行比较 - Compare Text Box field to Server-side DB using ASP.NET (VB.NET) 如何将按钮重定向到日历属性,并在vb.net中用所选日期填充文本框? - how to redirect a button to calendar properties and fill the text box with the selected date in vb.net? 如何获取列表框vb.net的选定值字段 - how to get the selected value field of listbox vb.net ASP.NET VB.NET-如何将登录到当前用户名的Active Directory填充到文本框字段中 - ASP.NET VB.NET - How to populate Active Directory Logged in Current User's Name into Text Box Field 使用VB.Net使用SqlDatasource编辑Gridview w / o - Editing a Gridview w/o using an SqlDatasource using VB.Net 在(vb.net)后面的代码中更改SqlDataSource ConnectionStrings值 - Change SqlDataSource ConnectionStrings value in code behind (vb.net) 使用VB.NET在“文本”框中添加属性 - Adding Attribute in Text box using VB.NET 在列表框中的文本框中显示文本-ASP.net&VB.net - Displaying Text in a Text Box From Listbox - ASP.net & VB.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM