[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2313
  • Last Modified:

asp.net bind textBox to an object

Hello together,

I want to bind a textBox to an object like the following.

Thank you in advance.
Public Class Test
    Private strTest As String
    Public Property usingTest() As String
        Get
            Return Me.strTest 
        End Get
        Set(ByVal value As String)
            Me.strTest = value
        End Set
    End Property
End class
 
'in another class I generate a TextBox
dim tst as new Test
dim txt01 as new TextBox
 
'something like this
'txt01.Text = "<%# Container.DataItem('tst.usingTest') %>"
'txt01.Text = "<%#tst.usingTest%>"

Open in new window

0
Gaiala
Asked:
Gaiala
  • 5
  • 4
1 Solution
 
Oliver AmayaEntrepeneurCommented:
Hi, something like this?
dim tst as new Test
dim txt01 as new TextBox
 
txt01.Text = tst.usingTest

Open in new window

0
 
GaialaAuthor Commented:
no that does not work.
0
 
Oliver AmayaEntrepeneurCommented:
Do you get an error?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
GaialaAuthor Commented:
I don't get an error.  The problem is that at this time tst.usingTest is always  emty.
The textbox belongs to a templatefield (gridView).
0
 
Oliver AmayaEntrepeneurCommented:
Ok, but when are you setting it's value? the code I showed was just an example of how to call the usingTest property. At some point you would need to do something like this:
tst.usingTest = "Some Value"

Open in new window

0
 
GaialaAuthor Commented:
here my code
'the class FillObjects
For Each ndChild As XmlNode In ndItem.ChildNodes
   xmlTest = New GridContents
   '...
   xmlTest.stateProperty() = tempString 'has a value
   '....
 
'the class GridContents
 Public Property stateProperty() As String()
        Get
            Return Me.strState
        End Get
        Set(ByVal value() As String)
            Me.strState= value
        End Set
 End Property
 
'I have GridView which contains TemplateFields
 
Dim templField As New TemplateField
templField.HeaderText = strHeaderText
templField.Visible = True
templField.ItemTemplate = New TestTemplate
testGrid .Columns.Add(templField)
 
'the implementation of TestTemplate looks like
Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim txtControl As New TextBox
        Dim lblControl As New Label
         lblControl.ID = "lblCon"
        txtControl.ID = "txtBox"
        Dim cl_TC As New GridContents
        Dim tempStr() As String
      
        txtControl.Text = cl_TC.StateProperty 'the value is always empty
        container.Controls.Add(lblControl) 
        container.Controls.Add(txtControl)
 End Sub

Open in new window

0
 
Oliver AmayaEntrepeneurCommented:
It's empty because you haven't assigned anything to it, in you InstantiateIn method you create a new object of type GridContents and that's it, you don't do anything else with it, meaning that the StateProperty will always be empty.
Dim cl_TC As New GridContents
txtControl.Text = cl_TC.StateProperty 'the value is always empty

Open in new window

0
 
GaialaAuthor Commented:
oh sure, you are right!!!
so where is no possibility to get the already instantiated objects to bind on the textbox?
0
 
Oliver AmayaEntrepeneurCommented:
Depends on what you're doing after you set the value in the code below, is this getting saved to an xml file after you set the value? if so you would have to read the contents of that XML file whenever you want to set the textbox's value.
For Each ndChild As XmlNode In ndItem.ChildNodes
   xmlTest = New GridContents
   '...'
   xmlTest.stateProperty = tempString 'has a value'
   '....'

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now