Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Few months ago I've been seeing many users in the forum asking how to generate dynamic textbox on the form and save the values to the database on Button click, so I've decided to write this post and wrap up all the examples that I wrote before on how to accomplish the task using different controls so that when I encounter such or similar questions again in the forum, I can simply refer them to this post.

Here are those examples:

Using ASP Table:

FAQ: Dynamically Adding Rows in ASP Table on Button Click event

Dynamically Adding TextBox Control to ASPNET Table

Using GridView:

Insert Values  from Dynamic Columns to Database

Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes

For Deleting of rows you can refer here: http://geekswithblogs.net/dotNETvinz/archive/2009/08/12/updated-adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes-again.aspx

For Saving the dynamic data to database you can refer here:

http://www.aspsnippets.com/post/2009/08/16/Save-and-Retrieve-Dynamic-TextBox-values-in-GridView-to-SQL-Server-Database.aspx


Hope you will find this post useful!

Posted on Wednesday, January 27, 2010 7:49 PM ASP.NET | Back to top


Comments on this post: FAQ: How to- Generate Dynamic TexBox in the Form and Save the values to Database

# re: FAQ: How to- Generate Dynamic TexBox in the Form and Save the values to Database
Requesting Gravatar...
Hi,
I am trying to use a listbox instead of a textbox. I am trying to select multiple values in the listbox and trying to save the values (comma separated)in a single record in the database table. I am unable to save it as only the first selected value in listbox is saved in the database table. Please find the updated code below in code behind below:

Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim rowIndex As Integer = 0
Dim strItems As String
Dim SelectedItems As String = String.Empty
Dim sc As New StringCollection()

If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
'extract the TextBox values
Dim itemIndex As Integer = 0
Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
Dim box2 As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlStatus"), DropDownList)
Dim box3 As ListBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("lstText"), ListBox)

Dim lstItem As ListItem
For Each lstItem In box3.Items

If lstItem.Selected Then
strItems = lstItem.Text
If itemIndex <> 0 Then
strItems = ", " + strItems
End If
SelectedItems += strItems
itemIndex += 1
End If
Next



'get the values here

'Response.Write(box1.Text + "<BR/>")
'Response.Write(box2.Text + "<BR/>")
'Response.Write(box3.Text)

sc.Add((ddlProj.SelectedValue & ",") + ((box1.Text & ",") + box2.SelectedValue & ",") + SelectedItems)
rowIndex += 1


Next
InsertRecords(sc)
'Next
End If
End If
Left by Dot Net Rookie on Apr 06, 2010 9:49 PM

# re: FAQ: How to- Generate Dynamic TexBox in the Form and Save the values to Database
Requesting Gravatar...
Hi,
I am using your code to dynamically generate textbox and saving it to db. I am using a listbox and I am binding the listbox with values frm code behind in Gridview Row Updating instead of SqlDataSource. But the values are not binded in the listbox. Kindly enlighten me .....
Left by Bill Patterson on Jun 25, 2010 7:02 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net