单击按钮时 ModalPopupExtender 丢失动态添加的 table 行
ModalPopupExtender loses dynamically-added table rows on button click
VS 2013, ASP.NET 3.5 VB.NET
页面有多个用于不同弹出窗口的 ModalPopupExtender 控件。其中一个弹出窗口用 CheckBoxControls 和追加销售项目的描述填充 table。
页面代码:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
[snip]
<ajax:ModalPopupExtender
runat="server"
ID="mpeUpsell"
popupControlID="pUpsell"
CancelControlID="bGoBack"
BackgroundCssClass="custBackground"
PopupDragHandleControlID="phDragUpsell"
TargetControlID="bHidden"
Enabled="True" />
<asp:Panel runat="server" ID="pUpsell" style="display:none;">
<div runat="server" class="custPopup">
<div runat="server" id="phDragUpsell">
<h3 style="text-align:center;">
<asp:Label
ID="lUpsellHdr"
runat="server"
Text="Check Out these Add-Ons" />
</h3>
</div>
<asp:Label
ID="lAddOnsText"
runat="server" />
[snip]
<asp:Table
ID="tItems"
runat="server"
style="border: 1px solid black;">
[snip] <asp:Button ID="bNext" runat="server" Text="Next --->" />
</td>
</tr>
</table>
<br />
<br />
</div>
</asp:Panel>
table tItems 的填充方式如下:
For Each dr As DataRow In ds.Tables(0).Rows
tr = New TableRow()
tc = New TableCell()
Dim sName As String = "cbItem_" & CInt(dr("ITEM_ID"))
Dim cb As New CheckBox()
cb.ID = sName
cb.Text = dr("ITEM_DESC").ToString.Trim
If CInt(dr("UGICheckOFF")) = 1 Then
cb.Checked = True
Else
cb.Checked = False
End If
[snip formatting]
tc.Controls.Add(cb)
tr.Cells.Add(tc)
tc2 = New TableCell()
Dim sPrice As String = String.Format("{0:C}", dr("ITEM_PRICE"))
tc2.Text = sPrice
[snip formatting]
tr.Cells.Add(tc2)
tItems.Rows.Add(tr)
Next
当我使用代码隐藏显示 mpeUpsell ModalPopupExtender 时,table 完美填充并显示。
bNext 按钮的处理程序如下所示:
Protected Sub bNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bNext.Click
If iItemArray IsNot Nothing Then iItemArray = Nothing ' get rid of it
' Handle the Next Button
Dim iCartID As Integer = -1
iCartID = CInt(Session("CartID"))
' iterate the items and add each to cart if not already in cart
iItemArray = New ArrayList()
For Each tr As TableRow In tItems.Rows
For Each tc As TableCell In tr.Cells
For Each ctl As Control In tc.Controls
If TypeOf (ctl) Is CheckBox Then
Dim cb As CheckBox = CType(ctl, CheckBox)
If cb.Checked Then
'if there are checks create array of item IDs checked.
Dim itemid As Integer = CInt(cb.ID.Replace("cbItem_", ""))
iItemArray.Add(itemid)
End If
End If
Next
Next
Next
[snip]
End Sub
执行处理程序时,tItems.Rows.Count 为零并且 ForEach 简单地失败,因为 table 行已经消失。
很明显我漏掉了什么;我只是不知道我错过了什么。
谢谢,
约翰.
解决方案是放弃 asp table,因为在这种情况下它不是有状态的。相反,我使用了 gridview 并将数据table绑定到它,并且能够检索值。
谢谢。
VS 2013, ASP.NET 3.5 VB.NET
页面有多个用于不同弹出窗口的 ModalPopupExtender 控件。其中一个弹出窗口用 CheckBoxControls 和追加销售项目的描述填充 table。
页面代码:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
[snip]
<ajax:ModalPopupExtender
runat="server"
ID="mpeUpsell"
popupControlID="pUpsell"
CancelControlID="bGoBack"
BackgroundCssClass="custBackground"
PopupDragHandleControlID="phDragUpsell"
TargetControlID="bHidden"
Enabled="True" />
<asp:Panel runat="server" ID="pUpsell" style="display:none;">
<div runat="server" class="custPopup">
<div runat="server" id="phDragUpsell">
<h3 style="text-align:center;">
<asp:Label
ID="lUpsellHdr"
runat="server"
Text="Check Out these Add-Ons" />
</h3>
</div>
<asp:Label
ID="lAddOnsText"
runat="server" />
[snip]
<asp:Table
ID="tItems"
runat="server"
style="border: 1px solid black;">
[snip] <asp:Button ID="bNext" runat="server" Text="Next --->" />
</td>
</tr>
</table>
<br />
<br />
</div>
</asp:Panel>
table tItems 的填充方式如下:
For Each dr As DataRow In ds.Tables(0).Rows
tr = New TableRow()
tc = New TableCell()
Dim sName As String = "cbItem_" & CInt(dr("ITEM_ID"))
Dim cb As New CheckBox()
cb.ID = sName
cb.Text = dr("ITEM_DESC").ToString.Trim
If CInt(dr("UGICheckOFF")) = 1 Then
cb.Checked = True
Else
cb.Checked = False
End If
[snip formatting]
tc.Controls.Add(cb)
tr.Cells.Add(tc)
tc2 = New TableCell()
Dim sPrice As String = String.Format("{0:C}", dr("ITEM_PRICE"))
tc2.Text = sPrice
[snip formatting]
tr.Cells.Add(tc2)
tItems.Rows.Add(tr)
Next
当我使用代码隐藏显示 mpeUpsell ModalPopupExtender 时,table 完美填充并显示。
bNext 按钮的处理程序如下所示:
Protected Sub bNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bNext.Click
If iItemArray IsNot Nothing Then iItemArray = Nothing ' get rid of it
' Handle the Next Button
Dim iCartID As Integer = -1
iCartID = CInt(Session("CartID"))
' iterate the items and add each to cart if not already in cart
iItemArray = New ArrayList()
For Each tr As TableRow In tItems.Rows
For Each tc As TableCell In tr.Cells
For Each ctl As Control In tc.Controls
If TypeOf (ctl) Is CheckBox Then
Dim cb As CheckBox = CType(ctl, CheckBox)
If cb.Checked Then
'if there are checks create array of item IDs checked.
Dim itemid As Integer = CInt(cb.ID.Replace("cbItem_", ""))
iItemArray.Add(itemid)
End If
End If
Next
Next
Next
[snip]
End Sub
执行处理程序时,tItems.Rows.Count 为零并且 ForEach 简单地失败,因为 table 行已经消失。
很明显我漏掉了什么;我只是不知道我错过了什么。
谢谢, 约翰.
解决方案是放弃 asp table,因为在这种情况下它不是有状态的。相反,我使用了 gridview 并将数据table绑定到它,并且能够检索值。
谢谢。