By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,208 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,208 IT Pros & Developers. It's quick & easy.

add value to database from dynamically created textbox

P: 90
I have created only one object for the textbox and that particular textbox is being repeated as many times I want.But if I want to add the values in the database from each and every textbox which have been dynamically created how should I do that? I have uesd dropdown box to get the number of textboxes to be displayed and used panel to display them.
Let me explain with my real time scenario , if customer-A has 2 telephone lines and customer-B has 3 telephone lines then the textbox are displayed as required but how to add these values to DataBase from dynamically created textboxes?
thanx in advance
Dec 18 '07 #1
Share this Question
Share on Google+
3 Replies


nateraaaa
Expert 100+
P: 663
Do the textboxes that you create dynamically contain an id property for each textbox? If not I would recommend that you use a counter variable or some other way to distinguish which textbox is which. Then simply reference the Text property of the textbox and pass that string value to your database as a parameter in a stored procedure.

Nathan
Dec 18 '07 #2

Frinavale
Expert Mod 5K+
P: 9,731
I have created only one object for the textbox and that particular textbox is being repeated as many times I want.But if I want to add the values in the database from each and every textbox which have been dynamically created how should I do that? I have uesd dropdown box to get the number of textboxes to be displayed and used panel to display them.
Let me explain with my real time scenario , if customer-A has 2 telephone lines and customer-B has 3 telephone lines then the textbox are displayed as required but how to add these values to DataBase from dynamically created textboxes?
thanx in advance
How are you dynamically displaying the textboxes?
Dec 18 '07 #3

Frinavale
Expert Mod 5K+
P: 9,731
I have an example for you:

Expand|Select|Wrap|Line Numbers
  1. Private textBoxArr(6) As TextBox
  2.  
  3. Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  4.     'This event happens before any controls are initialized by ASP.NET
  5.     'The ViewState for objects has not been loaded.
  6.     'After this event happens, the ViewState is loaded for each control and the object's properties are filled with the values submitted.
  7.  
  8.     'Creating the TextBox Objects that are dynamically shown in the web page
  9.     'according to the number selected from a DropDownList
  10.         For x As Integer = 0 To textBoxArr.Length - 1
  11.             textBoxArr(x) = New TextBox
  12.             textBoxArr(x).ID = "myTextBox" + x.ToString
  13.             textBoxArr(x).Visible = False 'Initializing the TextBox so that it is not rendered in the browser 
  14.             Pnl_TextBoxes.Controls.Add(textBoxArr(x)) 'Adding the TextBox to the Panel that holds the text boxes.
  15.         Next
  16.  
  17. End Sub
  18.  
  19. Protected Sub numTextBoxes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numTextBoxes.SelectedIndexChanged
  20.         For x As Integer = 0 To numTextBoxes.SelectedValue - 1
  21.             textBoxArr(x).Visible = True 'Setting the TextBox to True
  22.         Next
  23. End Sub
  24.  
  25. Private Sub btnText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnText.Click
  26.  
  27.         Dim str As New StringBuilder
  28.  
  29.         For i As Integer = 0 To textBoxArr.Length - 1
  30.             If Not textBoxArr(i) Is Nothing Then
  31.                 str.Append(" " + textBoxArr(i).Text) 'Grabbing the text from the TextBox...remember that at this stage the TextBox's Text property has already been set by ASP.NET according to its ViewState
  32.             End If
  33.         Next
  34.  
  35.     'Showing in a label what was in the TextBoxes
  36.         lbl_message.Text = "message: " + str.ToString
  37.     'Instead of showing these values, you should store them in your database.
  38.     End Sub
  39.  
Dec 18 '07 #4

Post your reply

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