Asked by:
Delete a raw from a table by dynamically created button

Question
-
Hello,
I've been new to vb asp.net web development and I'm looking for a solution for this issue since a week. but still, the situation is same.
what I want to do is, I need to delete the selected row which has been created dynamically in the web page. I have been trying several methods. but none of these are working.
this is my codes and sample screen.
Imports System.Data Partial Class add_stock Inherits System.Web.UI.Page Private numOfColumns As Integer = 1 Private ctr As Integer = 0 Private table As Table = Nothing Dim row As New TableRow() Dim cell As New TableCell() Dim tb9 As New Button Dim panel2 As New Panel Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then table = New Table() table.ID = "tableBuild" Session("table") = table ViewState("ctr") = ctr End If ctr = CType(ViewState("ctr"), Int32) table = DirectCast(Session("table"), Table) Panel1.Controls.Add(table) End Sub Private Sub GenerateTable(ByVal colsCount As Integer) ctr += 1 Dim tb As New TextBox() tb.Text = TextBox21.Text tb.ID = "TextBoxRow_" & ctr tb.Width = 80 tb.Enabled = False cell.Controls.Add(tb) row.Cells.Add(cell) Dim cell1 As New TableCell() Dim tb1 As New TextBox() tb1.Text = TextBox19.Text tb1.ID = "TextBox1Row_" & ctr tb1.Width = 80 tb1.Enabled = False cell1.Controls.Add(tb1) row.Cells.Add(cell1) Dim cell2 As New TableCell() Dim tb2 As New TextBox() tb2.Text = TextBox22.Text tb2.ID = "TextBox2Row_" & ctr tb2.Width = 80 tb2.Enabled = False cell2.Controls.Add(tb2) row.Cells.Add(cell2) Dim cell3 As New TableCell() Dim tb3 As New TextBox() tb3.Text = TextBox17.Text tb3.ID = "TextBox3Row_" & ctr tb3.Width = 80 tb3.Enabled = False cell3.Controls.Add(tb3) row.Cells.Add(cell3) Dim cell4 As New TableCell() Dim tb4 As New TextBox() tb4.Text = TextBox23.Text tb4.ID = "TextBox4Row_" & ctr tb4.Width = 80 tb4.Enabled = False cell4.Controls.Add(tb4) row.Cells.Add(cell4) Dim cell5 As New TableCell() Dim tb5 As New TextBox() tb5.Text = TextBox20.Text tb5.ID = "TextBox5Row_" & ctr tb5.Width = 80 tb5.Enabled = False cell5.Controls.Add(tb5) row.Cells.Add(cell5) Dim cell6 As New TableCell() Dim tb6 As New TextBox() tb6.Text = TextBox18.Text tb6.ID = "TextBox6Row_" & ctr tb6.Width = 80 tb6.Enabled = False cell6.Controls.Add(tb6) row.Cells.Add(cell6) Dim cell7 As New TableCell() Dim tb7 As New TextBox() tb7.Text = DropDownList4.Text tb7.ID = "TextBox7Row_" & ctr tb7.Width = 80 tb7.Enabled = False cell7.Controls.Add(tb7) row.Cells.Add(cell7) Dim cell8 As New TableCell() Dim tb8 As New TextBox() tb8.Text = DropDownList3.Text tb8.ID = "TextBox8Row_" & ctr tb8.Width = 80 tb8.Enabled = False cell8.Controls.Add(tb8) row.Cells.Add(cell8) Dim cell9 As New TableCell() tb9.Text = "Delete" tb9.ID = ctr tb9.Width = 80 'tb9.Enabled = False 'panel2.Controls.Add(tb9) 'cell9.Controls.Add(tb9) 'row.Cells.Add(cell9) AddHandler tb9.Click, AddressOf Me.Button3_Click cell9.Controls.Add(tb9) row.Cells.Add(cell9) 'tb9 = New CheckBox() 'tb9.ID = "chkExample" 'tb9.Text = "Check / Uncheck" 'tb9.AutoPostBack = True 'AddHandler tb9.CheckedChanged, AddressOf Button3_Click 'Me.Form.Controls.Add(tb9) table.Rows.Add(row) Session("table") = table ViewState("ctr") = ctr End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' ClientScript.RegisterStartupScript(Me.GetType, "Button_Alert", "alert('Button clicked!');", True) numOfColumns = 1 'Generate the Table based from the inputs GenerateTable(numOfColumns) End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) ClientScript.RegisterStartupScript(Me.GetType, "Button_Alert", "alert('Button clicked!');", True) numOfColumns = 1 'Generate the Table based from the inputs GenerateTable(numOfColumns) End Sub Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click End Sub End Class
- Edited by hethuhamilage Friday, April 20, 2018 9:00 AM
- Moved by Cherry BuMicrosoft contingent staff Thursday, April 26, 2018 2:26 AM move from vb.net
Friday, April 20, 2018 8:58 AM
All replies
-
Most will point you to the ASP.Net forums, but I've seen an other contributor complaining that there are no answers given and certainly not on this kind of questions. While this is a real VB problem likewise the so often handled winform questions in this forum.
It has to do that a Webform has no state like a winform. It means that you have to get the flushed data again back into your table before you use it at postback.
Luckily you did put it in the session, so I guess the only thing you've to do is in the button events
table=session(table)
Maybe better that you start using 2018 conventions and start global values with an upper case, in this case Table.
Success
Cor
- Edited by Cor Ligthert Friday, April 20, 2018 9:16 AM
- Proposed as answer by Cherry BuMicrosoft contingent staff Monday, April 23, 2018 6:19 AM
Friday, April 20, 2018 9:13 AM