我曾经使用UpdatePanel作为整个Listview项的包装器。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
'....
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
<Triggers></Triggers>
</asp:UpdatePanel>
并按如下方式注册客户端脚本…
Private Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
if Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "OtherScript") Then
ScriptManager.RegisterStartupScript(DirectCast(Page.FindControl("UpdatePanel1"), UpdatePanel), GetType(String), "alertScript", "update('hpClips','false','inc')", True)
End If
End sub
现在我决定只包装ImageButtons组与更新面板如下…
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" RenderMode="Block">
<ContentTemplate>
<asp:ImageButton ID="btnAttach" runat="server" CommandName='<%# "AddC_" & Eval("QID") & "_" & Eval("Label") %>'/>
<asp:ImageButton ID="btnFavorite" runat="server" CommandName='<%# "AddF_" & Eval("QID") & "_" & Eval("Label") %>'/>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:ListView>
,我得到以下错误
Value cannot be null. Parameter name: control
执行ScriptManager.RegisterStartupScript(DirectCast(Page.FindControl("UpdatePanel1"), UpdatePanel), GetType(String), "alertScript", "update('hpClips','false','inc')", True)
我认为这与没有找到updatepanel控件的事实有关。直接强制转换抛出异常。那我该怎么解决呢?提前谢谢你。
更新:我也试过这个。(这一次,我没有得到异常,但客户端脚本没有执行)
Private Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
Dim UpdPanel As New UpdatePanel
For Each Up As UpdatePanel In e.Item.Controls.OfType(Of UpdatePanel)()
UpdPanel = Up
Next
if Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "OtherScript") Then
ScriptManager.RegisterStartupScript(DirectCast(UpdPanel, UpdatePanel), GetType(String), "alertScript", "update('hpClips','false','inc')", True)
End If
End sub
修复!下面的代码实现了这个技巧
If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "OtherScript") Then
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), Guid.NewGuid.ToString, "update('hpClips','false','inc')", True)
End If
有帮助的资源
ScriptManager的问题。注册startpscript在WebControl中嵌套在UpdatePanel
在异步回发期间使用RegisterStartupScript以编程方式添加JavaScript