Manipulating with Update Panel

Asked

Viewed 1,108 times

2

I’m working with asp:button e asp:Listbox using the event of click of asp:button to popular the listbox. I am trying to manage so that there is no more postBack in the event of click button, but the way I’m implementing is not having any effect with the updatePanel, would like a collaboration to be able to heal and learn from the case. The code follows below:

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>

Treatment with the Updatepanel:

<asp:ScriptManager ID="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 a Button Treatment:

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
  • I come here to inform you that I decided to treat the property of <xhtmlConformance mode="Legacy"/>

3 answers

1

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

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

Then Voce could create a global variable that would retain the status of bind of the data. It would start in false, and after the first bind of the information occurring, it would be set to true.

Whenever you click on the button to load the information, before pressing it makes a check of the variable that quarda the status of bind, and only charges if her status is false, so she would only carry one time, because at the end of the first load she would be set to true.

1

You can do this processing 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 the 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>

0

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

See in the example that Trigger inside the Updatepanel, this Rigger 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 my button, I call my button action :

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

}
  • I made the adaptations as directed, but the postback continues to happen. The fact that my button action is popular the listbox would not affect the functioning of Updatepanel?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.