Answered by:
Generate dynamic rows in asp.net+VB web page

Question
-
User810354248 posted
In my asp.net VB web. I want to add a add row on click facility. I searched a lot and found a sample. in C#. My page is in VB converted into VB and the VB version is not working .
C# version is adding rows dynamically. But VB version is not functional
I want also to add the data fed in the rows to be inserted into database.
Aspx (C#) page
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add New Row" />
C# Code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default5 : System.Web.UI.Page { //A global variable that will hold the current number of Rows //We set the values to 1 so that it will generate a default Row when the page loads private int numOfRows = 1; protected void Page_Load(object sender, EventArgs e) { //Generate the Rows on Initial Load if (!Page.IsPostBack) { GenerateTable(numOfRows); } } protected void Button1_Click(object sender, EventArgs e) { if (ViewState["RowsCount"] != null) { numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString()); GenerateTable(numOfRows); } } private void SetPreviousData(int rowsCount, int colsCount) { Table table = (Table)Page.FindControl("Table1"); if (table != null) { for (int i = 0; i < rowsCount; i++) { for (int j = 0; j < colsCount; j++) { //Extracting the Dynamic Controls from the Table TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j); //Use Request objects for getting the previous data of the dynamic textbox tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j]; } } } } private void GenerateTable(int rowsCount) { //Creat the Table and Add it to the Page Table table = new Table(); table.ID = "Table1"; Page.Form.Controls.Add(table); //The number of Columns to be generated const int colsCount = 3;//You can changed the value of 3 based on you requirements // Now iterate through the table and add your controls for (int i = 0; i < rowsCount; i++) { TableRow row = new TableRow(); for (int j = 0; j < colsCount; j++) { TableCell cell = new TableCell(); TextBox tb = new TextBox(); // Set a unique ID for each TextBox added tb.ID = "TextBoxRow_" + i + "Col_" + j; // Add the control to the TableCell cell.Controls.Add(tb); // Add the TableCell to the TableRow row.Cells.Add(cell); } // And finally, add the TableRow to the Table table.Rows.Add(row); } //Set Previous Data on PostBacks SetPreviousData(rowsCount, colsCount); //Sore the current Rows Count in ViewState rowsCount++; ViewState["RowsCount"] = rowsCount; } }
VB Code
Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Partial Class a_add_row Inherits System.Web.UI.Page Private numOfRows As Integer = 1 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Page.IsPostBack Then GenerateTable(numOfRows) End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) If ViewState("RowsCount") IsNot Nothing Then numOfRows = Convert.ToInt32(ViewState("RowsCount").ToString()) GenerateTable(numOfRows) End If End Sub Private Sub SetPreviousData(ByVal rowsCount As Integer, ByVal colsCount As Integer) Dim table As Table = CType(Page.FindControl("Table1"), Table) If table IsNot Nothing Then For i As Integer = 0 To rowsCount - 1 For j As Integer = 0 To colsCount - 1 Dim tb As TextBox = CType(table.Rows(i).Cells(j).FindControl("TextBoxRow_" & i & "Col_" + j), TextBox) tb.Text = Request.Form("TextBoxRow_" & i & "Col_" + j) Next Next End If End Sub Private Sub GenerateTable(ByVal rowsCount As Integer) Dim table As Table = New Table() table.ID = "Table1" Page.Form.Controls.Add(table) Const colsCount As Integer = 3 For i As Integer = 0 To rowsCount - 1 Dim row As TableRow = New TableRow() For j As Integer = 0 To colsCount - 1 Dim cell As TableCell = New TableCell() Dim tb As TextBox = New TextBox() tb.ID = "TextBoxRow_" & i & "Col_" + j cell.Controls.Add(tb) row.Cells.Add(cell) Next table.Rows.Add(row) Next SetPreviousData(rowsCount, colsCount) rowsCount += 1 ViewState("RowsCount") = rowsCount End Sub End Class
Thursday, December 7, 2017 4:09 PM
Answers
-
User283571144 posted
Hi Baiju EP,
According to your codes, I found you don't add the Handles Me.Load method in the pageload method.So it will not generate the table when page loaded.
I suggest you could try to change the
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
to
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Then it will work well.
If you want to insert the data into database, you could firstly read the data into datatable then you could using SqlBulkCopy to insert the datatable into database.
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) If ViewState("RowsCount") IsNot Nothing Then numOfRows = Convert.ToInt32(ViewState("RowsCount").ToString()) Dim d1 As DataTable = New DataTable() d1.Columns.AddRange(New DataColumn() {New DataColumn("text1"), New DataColumn("text2"), New DataColumn("text3")}) Const colsCount As Integer = 3 For g As Integer = 0 To numOfRows - 1 Dim dr As DataRow = d1.NewRow() For j As Integer = 0 To colsCount - 1 dr(j) = Request.Form("TextBoxRow_" & g & "Col_" + j) Next d1.Rows.Add(dr) Dim i As Integer = 0 Next 'then you could use SqlBulkCopy to insert the datatable into sqldatabase '.... End If End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 8, 2017 6:39 AM
All replies
-
User2103319870 posted
You need to conver the double values to string before concatenating. Change the SetPreviousData and GenerateTable method like below
Private Sub SetPreviousData(ByVal rowsCount As Integer, ByVal colsCount As Integer) Dim table As Table = CType(Page.FindControl("Table1"), Table) If table IsNot Nothing Then For i As Integer = 0 To rowsCount - 1 For j As Integer = 0 To colsCount - 1 Dim tb As TextBox = CType(table.Rows(i).Cells(j).FindControl("TextBoxRow_" & i.ToString() & "Col_" + j.ToString()), TextBox) tb.Text = Request.Form("TextBoxRow_" & i.ToString() & "Col_" + j.ToString()) Next Next End If End Sub Private Sub GenerateTable(ByVal rowsCount As Integer) Dim table As Table = New Table() table.ID = "Table1" Page.Form.Controls.Add(table) Const colsCount As Integer = 3 For i As Integer = 0 To rowsCount - 1 Dim row As TableRow = New TableRow() For j As Integer = 0 To colsCount - 1 Dim cell As TableCell = New TableCell() Dim tb As TextBox = New TextBox() tb.ID = "TextBoxRow_" & i.ToString() & "Col_" + j.ToString() cell.Controls.Add(tb) row.Cells.Add(cell) Next table.Rows.Add(row) Next SetPreviousData(rowsCount, colsCount) rowsCount += 1 ViewState("RowsCount") = rowsCount End Sub
Thursday, December 7, 2017 4:40 PM -
User810354248 posted
Tried it but no error on button click and also no rows generated. After the rows are generated i want to insert the data to database also.
Friday, December 8, 2017 1:02 AM -
User283571144 posted
Hi Baiju EP,
According to your codes, I found you don't add the Handles Me.Load method in the pageload method.So it will not generate the table when page loaded.
I suggest you could try to change the
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
to
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Then it will work well.
If you want to insert the data into database, you could firstly read the data into datatable then you could using SqlBulkCopy to insert the datatable into database.
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) If ViewState("RowsCount") IsNot Nothing Then numOfRows = Convert.ToInt32(ViewState("RowsCount").ToString()) Dim d1 As DataTable = New DataTable() d1.Columns.AddRange(New DataColumn() {New DataColumn("text1"), New DataColumn("text2"), New DataColumn("text3")}) Const colsCount As Integer = 3 For g As Integer = 0 To numOfRows - 1 Dim dr As DataRow = d1.NewRow() For j As Integer = 0 To colsCount - 1 dr(j) = Request.Form("TextBoxRow_" & g & "Col_" + j) Next d1.Rows.Add(dr) Dim i As Integer = 0 Next 'then you could use SqlBulkCopy to insert the datatable into sqldatabase '.... End If End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 8, 2017 6:39 AM