问题描述
您好,
我是vb asp.net网站开发的新手,我正在寻找一周以来的解决方案。但情况仍然如此。
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
推荐答案
它必须这样做,Webform没有像winform那样的状态。这意味着你必须先将刷新后的数据恢复到你的表中,然后才能在回发中使用它。
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)
也许更好您开始使用2018惯例并以大写字母开始全局值,在本例中为表格。
Maybe better that you start using 2018 conventions and start global values with an upper case, in this case Table.
这篇关于通过动态创建的按钮从表中删除原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!