Manipulating with Update Panel

2

I'm working with asp:button e asp:Listbox using the click event of asp:button to populate the listbox. I'm trying to manage so that no more postback occurs in the click of the button event, but how I'm implementing is not working with updatePanel , I'd like a collaboration to be able to heal and learn from the case. The code is as follows:

HTML:

<%@ Register Src="../Modelos/Cabecalho.ascx" TagName="Cabecalho" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Lysis</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script></head>

TreatmentwithUpdatePanel:

<asp:ScriptManagerID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="updProcesso" UpdateMode="Conditional" runat="server" ChildrenAsTriggers="true">
        <%--<asp:UpdatePanel ID="updProcesso" runat="server" >--%>
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                            Campos Disponíveis
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:ListBox ID="lstOrigem" CssClass="DropDownlist" runat="server" Height="200px"
                                Width="250px" SelectionMode="Multiple"></asp:ListBox>
                        </td>
                        <td>
                            <asp:Button ID="btnAcima" CssClass="Botao" runat="server" Text="Mover para Cima"
                                Width="110px" /><br />
                            <br />
                            <asp:Button ID="btnAbaixo" CssClass="Botao" runat="server" Text="Mover para Baixo"
                                Width="110px" /><br />
                            <br />
                            <asp:Button ID="btnSelecionar" CssClass="Botao" runat="server" Text="Selecionar"
                                Width="110px" /><br />
                            <br />
                            <asp:Button ID="btnDesfazer" CssClass="Botao" runat="server" Text="Desfazer Seleção"
                                Width="110px" />
                        </td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td>
                            Campos Selecionados
                        </td>
                        <td>
                            Ordem
                        </td>
                        <td>
                        </td>
                        <td>
                            Operação
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:ListBox ID="lstDestino" CssClass="DropDownlist" runat="server" Height="200px"
                                Width="250px" SelectionMode="Multiple"></asp:ListBox>
                        </td>
                        <td>
                            <asp:ListBox ID="lstOrdem" CssClass="DropDownlist" runat="server" Height="200px"
                                Width="50px"></asp:ListBox>
                        </td>
                        <td valign="top">
                            <asp:Button ID="btnMudaOrdem" CssClass="Botao" runat="server" Text="Mudar Ordem"
                                Width="95px" />
                        </td>
                        <td>
                            <asp:ListBox ID="lstOperacao" CssClass="DropDownlist" runat="server" Height="200px"
                                Width="115px"></asp:ListBox>
                        </td>
                        <td valign="top">
                            <asp:Button ID="btnMudaOperacao" CssClass="Botao" runat="server" Text="Mudar Operação"
                                Width="110px" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>

Example of handling a Button:

Protected Sub btnSelecionar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelecionar.Click

        Try

            ScriptManager1.RegisterPostBackControl(CType(sender, Control))

            If lstOrigem.SelectedIndex <> -1 Then

                If lstOrigem.SelectedIndex <> -1 Then

                    For item As Integer = lstOrigem.Items.Count - 1 To 0 Step -1

                        If lstOrigem.Items(item).Selected Then

                            Dim origem As New ListItem
                            origem.Text = "----"
                            origem.Value = lstOrigem.SelectedItem.Value

                            lstDestino.Items.Add(lstOrigem.SelectedItem)
                            lstOrdem.Items.Add(origem)

                            Dim operacao As New ListItem
                            operacao.Text = "----"
                            operacao.Value = lstOrigem.SelectedItem.Value

                            lstOperacao.Items.Add(operacao)
                            lstOrigem.Items.RemoveAt(lstOrigem.SelectedIndex)
                            lstDestino.ClearSelection()

                        End If

                    Next

                End If

                btnSelecionar.Focus()
                updProcesso.Update()

            End If

        Catch ex As Exception

            Session("erro") = ex
            Response.Redirect("../Paginas/Erro.aspx")

        End Try

    End Sub
    
asked by anonymous 24.12.2015 / 15:39

3 answers

1

You can do this via javascript the code below is an example of how to avoid a new postback (ajax) of the same "object" before the current one ends.

Insert after form runat="server"

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
<script type="text/javascript"> 
    var control_ = null, inst_ = Sys.WebForms.PageRequestManager.getInstance(); 
    function BeginRequestHandler(sender, args) { 
         control_ = args.get_postBackElement();  //Controle que disparou o PostBack 
         control_.disabled = true; //Desabilita o controle
    } 
    function EndRequestHandler(sender, args) { 
         control_.disabled = false; //habilita o controle
         control_ = null; 
    }
    inst_.add_beginRequest(BeginRequestHandler); //início da requisição
    inst_.add_endRequest(EndRequestHandler); //fim da requisição
</script>
    
29.12.2015 / 21:12
1

In this scenario I would not know how to work, however we could approach it in a different way.

Create a div with runat=server , so in onload of the page you can hide it via codebehind , or if you prefer, you can not use runat=server in the tag and hide it via jQuery .

So you could create a global variable that would hold the status of bind of the data. It would start at false , and after the first bind of information occurs, it would be set to true .

Whenever you click on the button to load the information, before loading it checks the variable that stops the status of bind , and only loads if its status is false , so it would only load one once, because at the end of the first load it would be set to true .

    
28.12.2015 / 14:09
0

You can use a Trigger on the button, an example below of a problem that I solved with this feature because of the postback:

See in the example that Trigger within the UpdatePanel, this trigger is referencing my button to trigger the Onclick action.

 <asp:UpdatePanel runat="server" ID="fl_upload" UpdateMode="Conditional">
                            <ContentTemplate>
                                <div class="col-lg-4">
                                    <asp:FileUpload runat="server" ID="fl_arquivos" CssClass="btn btn-primary" AllowMultiple="true"/>
                                    </div>
                                <div class="col-lg-1">
                                    <asp:Button runat="server" ID="btn_upload" OnClick="btn_upload_Click" Text="Enviar" CssClass="btn btn-primary"/>
                                    </div>
                            </ContentTemplate>
                            <Triggers>
                                <asp:PostBackTrigger ControlID="btn_upload" />
                            </Triggers>
                        </asp:UpdatePanel>

After I click on my button, I call the action of my button:

protected void btn_upload_Click(object sender, EventArgs e)
{
            //AÇÃO DO MEU BOTÃO
            //E para finalizar atualizo o UpdatePanel
            fl_upload.Update();

}
    
02.01.2016 / 12:32