Hej, Du måste återskapa dynamiskt genererade kontroller vid postback.Tabell försvinner vid postback
Jag genererar en tabell dynamiskt(tblBox).
I varje tabellcell skapar jag en dynamisk ImageButton.
Sen lägger jag till en Handler som hanterar klick på min ImageButton, "DoViewClick"
Typ så här:
AddHandler lbtnTrans.Command, AddressOf DoViewClick
DoViewClick ser ut så här, den gör inte så mycket än:
Private Sub DoViewClick(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Dim item_number As String = e.CommandArgument()
Dim myArr() As String = Split(item_number, ",")
txtSampleId.Text = item_number
txtColumn.Text = myArr(0).ToString()
txtRow.Text = myArr(1).ToString()
End Sub
Men när jag klickar på en ImageButton i tabellen, så försvinner tabellen.
Hur kan detta komma sig?`
Jag har satt "tblBox.EnableViewState = True".
Någon som kan hjälpa mig?
Jag vet inte om det spelar någon roll hur jag genererar min tabell, men så här ser den koden ut, om det hjälper.
Private Sub DrawSelectedBox(ByVal rows As Integer, ByVal cols As Integer, ByVal storageUnitObjectId As Integer)
Dim iTableWidth As Integer
Dim iCellCounter As Integer
Dim iRowCounter As Integer
Dim iCellHeightAndWidth As Integer
Dim sCellStyle As String
Dim lbtnTrans As System.Web.UI.WebControls.ImageButton
Dim isOccupied As Boolean
Dim sCellClassName As String
Dim obj As Business_Services.boStorageUnit
Dim tdsContent As tdsContent
Dim tdrContent As tdsContent.CONTENTRow
Dim dvContent As DataView
obj = New Business_Services.boStorageUnit
'Get number of cols and rows for current storage unit
tdsContent = obj.getContents(storageUnitObjectId)
iCellCounter = 1
iRowCounter = 1
tblBox.EnableViewState = True
iTableWidth = 300
iCellHeightAndWidth = CInt(iTableWidth / cols)
tblBox.Width = New Unit(iTableWidth)
Dim newRow As System.Web.UI.WebControls.TableRow
Dim newColumn As System.Web.UI.WebControls.TableCell
While iRowCounter <= rows
newRow = New System.Web.UI.WebControls.TableRow
While iCellCounter <= cols
dvContent = New DataView
dvContent = tdsContent.Tables(0).DefaultView
dvContent.RowFilter = "RowPos = " & CStr(iRowCounter) & " AND ColPos = " & CStr(iCellCounter)
If dvContent(0)("Occupied") Then
isOccupied = True
sCellClassName = "PlaceSampleCellOccupied"
Else
isOccupied = False
sCellClassName = "PlaceSampleCellFree"
End If
newColumn = New System.Web.UI.WebControls.TableCell
newColumn.Width = New Unit(iCellHeightAndWidth)
newColumn.Height = New Unit(iCellHeightAndWidth)
newColumn.HorizontalAlign = HorizontalAlign.Center
newColumn.VerticalAlign = VerticalAlign.Middle
newColumn.CssClass = sCellClassName
newColumn.Attributes.Add("style", GetCellBorders(iCellCounter, iRowCounter))
If Not isOccupied Then
newColumn.Attributes.Add("onmouseover", "this.className='PlaceSampleCellHover';")
newColumn.Attributes.Add("onmouseout", "this.className='';")
lbtnTrans = New System.Web.UI.WebControls.ImageButton
lbtnTrans.ID = iCellCounter & "," & iRowCounter
lbtnTrans.ToolTip = "Click to place sample"
lbtnTrans.ImageUrl = "Images/trans.gif"
lbtnTrans.Width = New Unit(iCellHeightAndWidth)
lbtnTrans.Height = New Unit(iCellHeightAndWidth)
lbtnTrans.CommandArgument = iCellCounter & "," & iRowCounter
AddHandler lbtnTrans.Command, AddressOf DoViewClick
newColumn.Controls.Add(lbtnTrans)
End If
newRow.Cells.Add(newColumn)
iCellCounter += 1
dvContent.RowFilter = ""
dvContent = Nothing
End While
'Add row to table
tblBox.Rows.Add(newRow)
iCellCounter = 1
iRowCounter += 1
End While
End Sub
Tacksam för hjälp!
/Jocke
Sv: Tabell försvinner vid postback
Men här är en artikel som kanske kan hjälpa dig lite:
http://www.codeproject.com/aspnet/retainingstate.asp?df=100&forumid=14609&exp=0&select=1316748