473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Databinding to text boxes

I have no idea about how to bind data to text box.

I have a user details aspx form and i wnt to display the data entered by the user before to be displayed in the text box when user clicking the Edit profile button.

i.e the values from sql database should be bounded with the text boxes. There are more than 27 text boxes in the user profile form.
May 4 '10 #1
8 2329
Frinavale
9,735 Expert Mod 8TB
I'm not sure where to start.
Could you please post what you have tried so far to solve the problem so that we have something to help you with?

Thanks

-Frinny
May 4 '10 #2
tlhintoq
3,525 Expert 2GB
Original Poster: I don't know how to use a search engine. Could someone do it for me?
http://www.lmgtfy.com/?q=C%23+databi...UTF-8&oe=UTF-8
May 4 '10 #3
@Frinavale
Thanks Frinny for your response


In short i just want to bind some data with the text boxes in my aspx form. But the data which i need to bind with the text boxes is from my sql database .

Hope tht its clear to u, there are 27 text boxes in my page.

Waiting for your help...
May 5 '10 #4
Frinavale
9,735 Expert Mod 8TB
I'm still not sure where to start helping you.
If I were you I'd create a class used to represent your user.
I would retrieve the user's details from the database.
I would use reflection to loop through the properties of the user class to create textboxes used to display and gather the user information.

-Frinny
May 5 '10 #5
DaveRook
147 100+
At a guess

<asp:Textbox ID="id" runat="server" text='<%#Eval("DatabaseColumnName") %>' /> (note single quotes)

This assumes you've used set up a dataset

Dave
May 5 '10 #6
Frinavale
9,735 Expert Mod 8TB
See that's the thing...maybe I'm just lazy or something but dragging 27 TextBoxes onto the page and naming them all and then binding to them...

Seems like too much work.


It'd be easier to create a ListOf TextBoxes...
Use Reflection to loop through all of the User's properties.
Each property you find create a TextBox (setting the TextBox's ID = the property name so that you can reference it later). Add the newly created TextBox to the ListOf TextBoxes and to a control (like a panel) that will contain the user's information. (Do all of this in your Page Init Event).

Then in your page load event, the first time the page is loaded...call a method on your user class to "get" the user's information. This will populate the class with data....

Now typically I just use reflection again at this point to populate the TextBoxes with text....but I see no reason why you can't set up the binding to the properties when you're dynamically creating the TextBoxes. (I've never tried this, so it might be harder than I think...)

See there's the other thing (lazieness coming out again) but I've created a "Field" user control that has a Textbox and a "Prompt" (a label control) used for describing the what the TextBox represents. I dynamically add these user controls to the page instead of just TextBoxes (since it makes more sense).

Like I said I'm a little lazy and don't like it when some-thing that I need to display/retrieve data for has 100+ fields that I need to interact with...it's just easier to reflect than do this manually.

-Frinny
May 5 '10 #7
DaveRook
147 100+
Frinny

I don't think you appreciate how valuable posts like yours are! Thank you. By doing so, you're giving people (including myself) an insight into best practices, how to approach problems etc.

Thank you

Dave
May 5 '10 #8
Frinavale
9,735 Expert Mod 8TB
I'm not sure it's best-practices but over time I've found my programming style is changing to be a lot more abstract...working at a level where I don't care what the property names are when displaying/retrieving data from the user.


Normally I don't just give out my thoughts on the problems that I'm helping with because I've learned that my approach to solving problems is vastly different than the people asking about the problem. I find it's easier to get insight into how the user thinks to give them a solution that they can easily understand and is their style of programming.

Here is a snippet of what I was talking about earlier...the code is clean, and simple and easy to use/maintain.

I have a Web User Control that has a Label to display a prompt/text and a TextBox that is used to display and gather data. This control is called "FieldUserControl" for simplicity and has 2 important properties: the Text property (which gets and sets the TextBox.Text property) and the Prompt property (which gets and sets the Label.Text property). It has other properties for styling etc but we won't get into them because I want to keep my point clear.

Now say we have a page (or user control) and we want to let the user see/edit a bunch of Text Properties of a class "XYZ".

First of all I would declare a ListOf FieldUserControls (at Page level scope), used as a container for all the FieldUserControls required to let the user see/edit the XYZ's properties.

Then in the Page Init Event I would use Reflection (so you need to import the namespace) to rip through the all the Properties in XYZ, create a FieldUserControl for each one, give the FieldUserControl the ID that matches the property name, and add the FieldUserControl to the page and to the ListOf FieldUserControls:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Reflection
  2. '......
  3. Private _xyzFields As List(Of FieldUserControl) 'a container for all the FieldUserControls required to display/retrieve data for XYZ
  4. Private urlToFieldControl As String = "~/FieldUserControl.ascx" 'Path to the FieldUserControl
  5.  
  6. Private Sub MyPage_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  7.   _xyzFields= New List(Of FieldUserControl)
  8.   InstantiateXyzFields()
  9. End Sub
  10.  
  11. Private Sub InstantiateXyzFields() 
  12.   'Grabbing all of the properties for class XYZ
  13.   Dim xyzFieldProperties() As PropertyInfo = GetType(XYZ).GetProperties
  14.  
  15.   'Ripping through each XYZ property
  16.   For i As Integer = 0 To xyzFieldProperties.Length - 1
  17.     'Creating a FieldUserControl control to represent the property
  18.     Dim f As FieldUserControl= CType(Page.LoadControl(_urlToFieldControl), FieldUserControl)
  19.  
  20.    'Getting the property information
  21.     Dim pi As PropertyInfo = xyzFieldProperties(i)
  22.  
  23.    'Setting the ID of the FieldUserControl to the property name
  24.     f.ID = pi.name
  25.  
  26.    'Setting the Prompt for the FieldUserControl to the property name
  27.     f.Prompt = pi.name
  28.  
  29.     'Adding the FieldUserControl control to a Panel that is on the page where we want
  30.     'the FieldUserControls to be displayed 
  31.     PanelThatContainsXYZsFields.Controls.Add(f)
  32.  
  33.     'Adding the field control to the list of fields for later use
  34.     _xyzFields.Add(f)
  35.   Next
  36. End Sub
Now we have a FieldUserControl control (a Label-TextBox pair) for every property that is part of the XYZ class added to the page....prompt is set with the name of the property so that the user can identify what it is (what's really nice is that you can set up resources that contain prompts in the languages that your application supports...using the property names as the Key value for each prompt...that way you can retrieve the translated text/user friendly prompt for each property easily)

At this time we have FieldUserControls added to the page with prompts but the TextBoxes don't contain any information. Say XYZ has a method called "Populate" that goes to the database, retrieves the information, and populates the class's properties with the data retrieved.

I would call this method in the Page Load event only if isPostback=false (the first time the page is loaded). Then I would loop through all of the properties for the class and set the corresponding FieldUserControl's Text property:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  3.   If IsPostBack = False Then
  4.     DisplayXYZData()    
  5.   End If
  6. End Sub
  7.  
  8. Private Sub DisplayXYZData()
  9.   'Creating an instance of the XYZ class
  10.   Dim instanceOfXYZ As New XYZ
  11.  
  12.   'Populating the instance of the class with data
  13.   instanceOfXYZ.Populate()
  14.  
  15.   'Grabbing all of the properties for class XYZ
  16.   Dim xyzFieldProperties() As PropertyInfo = GetType(XYZ).GetProperties
  17.  
  18.   'Ripping through each XYZ property
  19.   For i As Integer = 0 To xyzFieldProperties.Length - 1   
  20.    'Getting the property information
  21.     Dim pi As PropertyInfo = xyzFieldProperties(i)
  22.  
  23.     'Getting the property name & storing it into a string (for Lambda expression to work nicely)
  24.     Dim propertyName As String = pi.Name
  25.  
  26.     'Using the Array.Find method to retrieve the FieldUserControl that corresponds with the property
  27.     'Note that I'm using a Lambda expression for simplicity sake
  28.     Dim f As FieldUserControl = Array.Find(_xyzFields.ToArray, Function(x) String.Compare(x.ID,propertyName)=0)
  29.  
  30.     'Making sure that the Array.Find method worked...that f exists before trying to use it
  31.     If f IsNot Nothing Then
  32.       'Setting the FieldUserControl's Text property with the value of the property
  33.       'that is in the instanceOfXYZ object
  34.        f.Text = pi.GetValue(instanceOfXYZ,Nothing)
  35.     End If
  36.   Next
  37. End Sub
  38.  
Tada: done.
We are displaying all properties of a class (whether it be 1 or 1000) on the screen with a Label for each property and a TextBox for each property. The TextBoxes are displaying data for the properties....what more could you ask for?

Oh retrieveing data is just as simple: get all the properties for the class, get the corresponding FieldUserControl for the property...use the property Info's SetValue method to set the class with the data in the TextBox.

Expand|Select|Wrap|Line Numbers
  1. Private Sub RetrieveXYZData()
  2.   'Creating an instance of the XYZ class
  3.   Dim instanceOfXYZ As New XYZ
  4.  
  5.   'Grabbing all of the properties for class XYZ
  6.   Dim xyzFieldProperties() As PropertyInfo = GetType(XYZ).GetProperties
  7.  
  8.   'Ripping through each XYZ property
  9.   For i As Integer = 0 To xyzFieldProperties.Length - 1   
  10.    'Getting the property information
  11.     Dim pi As PropertyInfo = xyzFieldProperties(i)
  12.  
  13.     'Getting the property name & storing it into a string (for Lambda expression to work nicely)
  14.     Dim propertyName As String = pi.Name
  15.  
  16.     'Using the Array.Find method to retrieve the FieldUserControl that corresponds with the property
  17.     'Note that I'm using a Lambda expression for simplicity sake
  18.     Dim f As FieldUserControl = Array.Find(_xyzFields.ToArray, Function(x) String.Compare(x.ID,propertyName)=0)
  19.  
  20.     'Making sure that the Array.Find method worked...that f exists before trying to use it
  21.     If f IsNot Nothing Then
  22.       'Setting the instanceOfXYZ's property with the value of the Text property
  23.       'that is in the corresponding FieldUserControl object
  24.       pi.SetValue(instanceOfXYZ, f.Text, BindingFlags.GetField, Nothing, Nothing, Nothing)
  25.     End If
  26.   Next
  27. End Sub

-Frinny
May 5 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JohnFol | last post by:
I have followed one of the walkthroughs that shows a parameterised query populating some text boxes that are data bound to a data set. The full details are found at ...
0
by: Ahmet GUNES | last post by:
Hi all, Although I tried several different ways, I wasn't able to succeed databinding a parameterized property. Can anyone help? Thanks in advance, AG In a form with two text boxes on...
1
by: Gary Shell | last post by:
I have a pair of combo boxes on a form. Both have their SelectedValue property bound to a column on a table called "Input_Output". One column is called "Class" and the second is called "SubClass"....
7
by: Richard | last post by:
I have a form with seven tapages. These span only one record with a large number of fields (textboxes). On Tabpage1 I display a number of read-only text boxes. This displays information about...
1
by: rb | last post by:
I'm a bit confused with this new data binding methodology - well, at least the "visual" part of it. With asp.net 1, I used be able to "mouse-my-way" around data binding instead of doing it in...
5
by: Dennis | last post by:
I am trying to create a form using databinding to a dataset and one of the fields requires the user to select from a list of optons. Any hints on how to do this other than bind the field to a...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
1
by: Wan | last post by:
Hi, I have a test project consists of two forms. The main form contains a datagrid and a button. On click of button I populate the datagrid with a ds.table(0) so far so good and on grid's...
0
by: Dave A | last post by:
I have a user control that is dynamically loaded into a repeater; (so the user control appears several times) The user control features a text box and a delete button. When the delete button...
0
by: hathan | last post by:
Hi Everyone, as you can see below i have sent data from a Access database to text boxes. I have used DataBinding to do this the problem i have is that databinding is readonly and i want to write to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.