Bind Textbox to DataSet

G

Guest

Hi. I have a dataset on my webform which I successfully fill by calling a
Sub that occurs after the Page_Load. My question is - I have a textbox
control which I need to populate with the contents of one of the Dataset
columns. If I set set the binding properties using the properties window,
this isn't successfull since the textbox attempts to bind at page_load, at
which time the dataset is not filled.

How can I use VB code to specify that the textbox's text property should be
set to a column within the dataset? Thanks!
 
J

Jeppe Dige Jespersen

How can I use VB code to specify that the textbox's text property should
be
set to a column within the dataset? Thanks!

If you're using a typed dataset (are you?), you can easily bind the textbox
to a column by setting the "databindings" property of the textbox to point
to a column in the typed dataset's defaultView.

You do this design-time.

Then, whenever you fill the typed dataset, you can afterwards call
myTextbox.databind(), which will get the data from the datatable.

Hope it helps,
Jeppe Jespersen
 
K

Ken Cox [Microsoft MVP]

Hi Mike,

You should be able to bind to a textbox by binding the page. Here's some
code that might give you the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset and an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function


<form id="Form1" method="post" runat="server">
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
<p>
<asp:label id="Label1" runat="server"></asp:label></p>
</form>
 
G

Guest

Thank you both! I have this working now after learning from your examples.
Thanks...

Ken Cox said:
Hi Mike,

You should be able to bind to a textbox by binding the page. Here's some
code that might give you the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset and an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function


<form id="Form1" method="post" runat="server">
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
<p>
<asp:label id="Label1" runat="server"></asp:label></p>
</form>

MrMike said:
Hi. I have a dataset on my webform which I successfully fill by calling a
Sub that occurs after the Page_Load. My question is - I have a textbox
control which I need to populate with the contents of one of the Dataset
columns. If I set set the binding properties using the properties window,
this isn't successfull since the textbox attempts to bind at page_load, at
which time the dataset is not filled.

How can I use VB code to specify that the textbox's text property should
be
set to a column within the dataset? Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top