I have a button click event that adds text to a table. However, when I submit the form the values entered into the table don't appear in the postback... is this possible to do?
Here is my code:
Protected Sub AddRowToInputTables(table As Table, value As String)
If table IsNot Nothing Then
Dim id As String = value.Trim()
Dim prefix As String = ""
If (table.ID = "cust_num") Then ' todo - abstract this out and use a parameter for the prefix instead
prefix = "CN"
Else
prefix = "RN"
End If
If id <> String.Empty Then
Dim tr = New TableRow()
Dim td = New TableCell()
开发者_开发知识库 Dim td2 = New TableCell()
Dim btnDelete = New ImageButton()
btnDelete.ID = "btn" & prefix & "_" & id & "_del"
btnDelete.ImageUrl = "http://res.xxxxxxxx.net/images/delete.png" ' todo - put this in the web.config or settings file
tr.ID = id
td.Text = id
td2.Controls.Add(btnDelete)
tr.Cells.Add(td)
tr.Cells.Add(td2)
Dim rows = New List(Of TableRow)()
For Each row In table.Rows
rows.Add(row)
Next
rows.Add(tr)
table.Rows.Clear()
table.Rows.AddRange(rows.ToArray())
cust_num_txt.Value = String.Empty
res_num_txt.Value = String.Empty
End If
End If
End Sub
You will either need to commit the table to ViewState or Session, or recreate the table every time the page is posted back. If the table isn't too large, you can probably just throw it in ViewState.
To save the table to ViewState:
ViewState["MyTable"] = table;
To retrieve the table from ViewState:
table = (DataTable)ViewState["MyTable"];
Trick is to populate the table in the Load event only... then everything started working normally.
I had the same problem and the following solution worked well :
sub AddRowsToTable()
session("MyTable") = Nothing
MyTable.Rows.Clear
'... Adding rows and cells here ...
end sub
Protected Sub btnRefreshTable_Click(sender As Object, e As ImageClickEventArgs) Handles btnRefreshTable.Click
AddRowsToTable()
Session("MyTable") = MyTable
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
If Not IsNothing(Session("MyTable")) Then
AddRowsToTable()
End If
End If
End Sub
hope this help
精彩评论