Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

databinding to textbox

Status
Not open for further replies.

GHOST24

Programmer
Jul 7, 2005
22
GB
I'm quite new to asp.net and would like to know how to bind data to a textbox.
I have done this many times in vb.net but the properties are different in asp can someone point me in the right direction please
 
Personally, I just set the Text property of textboxes when my data changes, as I think binding is overkill for most situations involving text boxes but it can be done:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
I ran into the same shock when I migrated for vb.net windows forms to web applicaions. Web apps do not have this built in like you do in windows apps. You can write your own in a utils class and use it almost the same as you do in win forms.

Here is a start.


This one populates the form:
Code:
Public Function AutoBindToForm(ByVal _Me As Control, ByVal tbl As DataTable) As DataTable
        Dim Rw As DataRow
        If tbl.Rows.Count = 0 Then
            Rw = tbl.NewRow
        Else
            Rw = tbl.Rows(0)
        End If

        Dim c As DataColumn
        Dim ctl As Object
        For Each c In tbl.Columns
            ctl = FindMyControlDeep(_Me, "bnd_" & c.ColumnName)
            If Not ctl Is Nothing Then
                PopulateControl(ctl, Rw, c)
            End If
        Next
        Return tbl
    End Function

Public Function FindMyControlDeep(ByVal p As Object, ByVal MyId As String) As Object
        Try
            Dim ctl As Object
            If p Is Nothing Then Return Nothing
            If p.HasControls Then
                Dim obj As Object
                ctl = p.FindControl(MyId)
                If Not ctl Is Nothing Then Return ctl
                For Each obj In p.Controls
                    ctl = FindMyControlDeep(obj, MyId)
                    If Not ctl Is Nothing Then Return ctl
                Next
            End If
        Catch
        End Try
        Return Nothing
    End Function


Public Function PopulateControl(ByVal Ctl As Object, ByVal Rw As DataRow, ByVal c As DataColumn) As Boolean
        If TypeOf Ctl Is TextBox Then
            Ctl.Text = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is Label Then
            Ctl.Text = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is RadioButton Then
            SetGroupValue(Ctl.Parent, Ctl.GroupName, Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is CheckBox Then
            Ctl.checked = CBool(Null2z(Rw(c.ColumnName), False))
            Return True
        ElseIf TypeOf Ctl Is System.Web.UI.HtmlControls.HtmlInputCheckBox Then
            Ctl.checked = CBool(Null2z(Rw(c.ColumnName), False))
            Return True
        ElseIf TypeOf Ctl Is System.Web.UI.HtmlControls.HtmlInputFile Then
            Ctl.value = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is System.Web.UI.HtmlControls.HtmlInputHidden Then
            Ctl.value = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is DropDownList Then
            Dim obj As DropDownList = CType(Ctl, DropDownList)
            SelectListBoxValue(obj, CStr(Null2z(Rw(c.ColumnName), "")))
            'obj.SelectedValue = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True

        ElseIf TypeOf Ctl Is ListBox Then
            Dim obj As ListBox = CType(Ctl, ListBox)
            'obj.SelectedValue = CStr(Null2z(Rw(c.ColumnName), ""))
            SelectListBoxValue(obj, CStr(Null2z(Rw(c.ColumnName), "")))
            Return True
        ElseIf Ctl.GetType.ToString = "Telerik.WebControls.RadEditor" Then
            Ctl.Html = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True
        ElseIf TypeOf Ctl Is System.Web.UI.HtmlControls.HtmlSelect Then
            Dim obj As System.Web.UI.HtmlControls.HtmlSelect = CType(Ctl, System.Web.UI.HtmlControls.HtmlSelect)
            SelectSelectOptionValue(obj, CStr(Null2z(Rw(c.ColumnName), "")))
            'obj.SelectedValue = CStr(Null2z(Rw(c.ColumnName), ""))
            Return True

        ElseIf TypeOf Ctl Is Web.UI.UserControl Then
            Dim ctl2 As Web.UI.UserControl = CType(Ctl, Web.UI.UserControl)
            If ctl2.HasControls Then
                Dim ctl3 As Object
                For Each ctl3 In ctl2.Controls
                    If PopulateControl(ctl3, Rw, c) Then
                        Return True
                    End If
                Next

            End If
        End If
    End Function

Public Sub SelectListBoxValue(ByRef MylistBox As Object, ByVal MyVal As Object)
        Dim obj As DropDownList = CType(MylistBox, DropDownList)
        Dim v As String = CStr(MyVal)
        If Not v = Nothing Then v = v.ToLower
        Dim i As Integer
        For i = 0 To obj.Items.Count - 1
            If CType(obj.Items(i), ListItem).Value.ToLower = v Then
                obj.SelectedIndex = i
                Exit For
            End If
        Next
    End Sub

These one gets the form.
Code:
Public Sub UpdateFromForm(ByVal _Me As Control, ByRef Tbl As DataTable) 
        Dim Rw As DataRow = Tbl.Rows(0)
        UpdateFromForm(_Me, Rw)
    End Sub


Public sub UpdateFromForm(ByVal _Me As Control, ByRef Rw As DataRow) 
        Dim c As DataColumn
        Dim ctl As Control
        Dim tbl As DataTable = Rw.Table
        For Each c In tbl.Columns
            ctl = FindMyControlDeep(_Me, "bnd_" & c.ColumnName)
            If Not ctl Is Nothing Then
                Dim Obj As Object
                If TypeOf ctl Is TextBox Then
                    Obj = CastDBType(CType(ctl, TextBox).Text, c)
                ElseIf TypeOf ctl Is Label Then
                    Obj = CastDBType(CType(ctl, Label).Text, c)
                ElseIf TypeOf ctl Is RadioButton Then
                    Dim ctl2 As RadioButton = CType(ctl, RadioButton)
                    Obj = CastDBType(GetGroupValue(_Me, ctl2.GroupName), c)
                ElseIf TypeOf ctl Is CheckBox Then
                    Obj = CastDBType(CType(ctl, CheckBox).Checked, c)
                ElseIf TypeOf ctl Is DropDownList Then
                    Obj = CastDBType(GetListBoxValue(CType(ctl, DropDownList)), c)
                ElseIf TypeOf ctl Is System.Web.UI.HtmlControls.HtmlSelect Then
                    Obj = CastDBType(GetListBoxValue(CType(ctl, System.Web.UI.HtmlControls.HtmlSelect)), c)
                ElseIf TypeOf ctl Is System.Web.UI.HtmlControls.HtmlInputFile Then
                    Obj = CastDBType(GetFileName(CType(ctl, System.Web.UI.HtmlControls.HtmlInputFile).Value), c)
                ElseIf TypeOf ctl Is System.Web.UI.HtmlControls.HtmlInputHidden Then
                    Obj = CastDBType(CType(ctl, System.Web.UI.HtmlControls.HtmlInputHidden).Value, c)
                ElseIf TypeOf ctl Is ListBox Then
                    Obj = CastDBType(GetListBoxValue(CType(ctl, ListBox)), c)
                ElseIf ctl.GetType.ToString = "Telerik.WebControls.RadEditor" Then
                    Obj = CastDBType(CType(ctl, Object).Html, c)
                End If

               Rw(c.ColumnName) = Obj
            End If
        Next
    End Sub

 Public Function CastDBType(ByVal s As Object, ByVal c As DataColumn, Optional ByVal DefaultVal As Object = Nothing) As Object
        If DefaultVal = Nothing Then DefaultVal = DBNull.Value
        If s = Nothing Then
            Return DefaultVal
        End If
        If TypeOf s Is String Then
            If s.length = 0 Then
                Return DefaultVal
            End If
        End If

        Dim t As System.Type = c.DataType
        If t.Name = "Boolean" Then
            If s = "1" Or s = 1 Then
                s = True
            End If
            If s = "0" Or s = 0 Then
                s = False
            End If
        End If

        Return Convert.ChangeType(s, t)
    End Function

You use it in your web form like:
Just name your controls with a "bnd_" naming convention:
<asp:textbox id="bnd_yourColumnName" runat=server />

Populate the form:
Dim tbl as new DataTable
YourDataAdapter.SelectCommand.CommandText+=" where id=" YourID ' you want a datatable with one record
YourDataAdapter.Fill(tbl)
AutoBindToForm(me,tbl)


Get the form:
Dim tbl as new DataTable
YourDataAdapter.SelectCommand.CommandText+=" where id=" YourID ' you want a datatable with one record
UpdateFromForm(Me, tbl)
YourDataAdapter.Update(tbl)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top