locked
Dynamic Button cannot be triggered if put within UpdatePanel. RRS feed

  • Question

  • User944339287 posted

    Hi guys.. my button cannot be triggered if i put within UpdatePanel. It's working as expected if i remove the UpdatePanel but it's needed for my web application. 

    Any idea?

    Code Behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim txt As New TextBox()
            txt.ID = ID
            txt.Text = "Guest Name"
            Me.pnlTextBoxes.Controls.Add(txt)
    
            Dim lt As New Literal()
            lt.Text = "<br />"
            Me.pnlTextBoxes.Controls.Add(lt)
    
            Dim btnSubmit As New Button
            btnSubmit.ID = "btnSubmit"
            btnSubmit.Text = "Submit"
            AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
    
            Me.pnlTextBoxes.Controls.Add(btnSubmit)
    
        End Sub
    
        Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim message As String = ""
            For Each textBox As TextBox In Me.pnlTextBoxes.Controls.OfType(Of TextBox)()
                message += textBox.ID + ": " + textBox.Text + "\n"
            Next
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
    
        End Sub

    HTML

    <asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
    <ContentTemplate>   
        <asp:Panel ID="pnlTextBoxes" runat="server">
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
    
    </Triggers>
    </asp:UpdatePanel>



    Tuesday, February 4, 2020 7:27 AM

All replies

  • User-719153870 posted

    Hi kengkit,

    my button cannot be triggered

    In fact, your button CAN be triggered but you can not see since the behavior you set in back code is a js function alert().

    Update below code and you will see what i'm talking about:

    For Each textBox As TextBox In Me.pnlTextBoxes.Controls.OfType(Of TextBox)()
                textBox.Text = "123"
                message += textBox.ID + ": " + textBox.Text + "\n"
            Next

    The problem is the js funtion you called in back code can not be triggered since there's an updatepanel in your page which requires all your functions be rebinded, check jQuery $(document).ready and UpdatePanels? and you will understand.

    Best Regard,

    Yang Shen

    Wednesday, February 5, 2020 7:38 AM