为自定义控件动态创建UpdatePanel会导致完整的回发

时间:2013-10-07 16:44:20

标签: asp.net ajax vb.net updatepanel

我现在已经被卡住了一天。我需要创建一个自定义控件,为了概念验证,我正在尝试创建UpdatePanel并在其中添加一个按钮。 它总是会导致整页回复

代码(自定义控件):

Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports AjaxControlToolkit

<Designer(GetType(HMXICDSearchToolDesigner))> _
Public Class HMXICDSearchTool
Inherits HMXISBaseCompositeControl
Implements INamingContainer

Private Const DEFAULT_WIDTH As Integer = 650
Private Const DEFAULT_HEIGHT As Integer = 450

Private _pnlUpdate As UpdatePanel = Nothing
Private _btnTest As Button = Nothing
Private _lbl As Label = Nothing

Protected Overrides Sub CreateChildControls()
    MyBase.CreateChildControls()

    _pnlUpdate = New UpdatePanel()
    With _pnlUpdate
        .ID = "pnlUpdate"
        .ChildrenAsTriggers = False
        .UpdateMode = UpdatePanelUpdateMode.Conditional
    End With

    _btnTest = New Button()
    With _btnTest
        .ID = "btnTest"
        .Text = "Test Button"
        .CausesValidation = False
        AddHandler .Click, AddressOf Btn_Click
    End With

    _lbl = New Label()
    With _lbl
        .ID = "lbl"
        .Text = "Hello!"
    End With

    _pnlUpdate.ContentTemplateContainer.Controls.Add(_btnTest)
    _pnlUpdate.ContentTemplateContainer.Controls.Add(_lbl)

    Dim aTrigger As AsyncPostBackTrigger = New AsyncPostBackTrigger()
    With aTrigger
        .ControlID = "btnTest"
        .EventName = "Click"
    End With

    Me._pnlUpdate.Triggers.Add(aTrigger)

    Me.Controls.Add(_pnlUpdate)

    Me.Style.Add("background-color", "red")
    Me.Height = 500
    Me.Width = 500
End Sub

Protected Sub Btn_Click(ByVal Sender As Object, ByVal e As System.EventArgs)
    If Not Me.Page.IsPostBack Then
        Me._lbl.Text = "Async postback."
    End If
End Sub
End Class

任何想法都将不胜感激。我试过切换ChildrenAsTriggers,把这段代码放在Me.Load等等。

1 个答案:

答案 0 :(得分:0)

它一直在执行部分回发。

我犯了测试的错误,看是否是这样的部分回发:

If Page.IsPostBack Then
   'something here
End

我本来应该检查这个

Me.ScriptManager.IsInAsyncPostback

这个:Page.IsPostBack即使正在执行部分回发,也会为真