Bind Textbox to DataSet

Discussion in 'ASP .Net' started by =?Utf-8?B?TXJNaWtl?=, Jan 24, 2005.

  1. 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!
     
    =?Utf-8?B?TXJNaWtl?=, Jan 24, 2005
    #1
    1. Advertising


  2. > 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
     
    Jeppe Dige Jespersen, Jan 24, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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!
     
    Ken Cox [Microsoft MVP], Jan 25, 2005
    #3
  4. Thank you both! I have this working now after learning from your examples.
    Thanks...

    "Ken Cox [Microsoft MVP]" wrote:

    > 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" <> wrote in message
    > news:...
    > > 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!

    >
    >
     
    =?Utf-8?B?TXJNaWtl?=, Jan 25, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    0
    Views:
    1,177
    HNguyen
    Jun 3, 2004
  2. Tea Cup
    Replies:
    0
    Views:
    544
    Tea Cup
    Aug 20, 2003
  3. MattB

    Newbie trying to bind to dataset

    MattB, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    586
    Eric Veltman
    Dec 22, 2003
  4. mr2_93
    Replies:
    1
    Views:
    3,602
    mr2_93
    Oct 2, 2005
  5. Replies:
    0
    Views:
    591
Loading...

Share This Page