Update Textbox without Postback

Asked

Viewed 801 times

0

I need to update the change textbox when the textbox receives customer value, is filled, I use this function inside the textbox receive customer value in the event Textchanged:

 protected void txtvalordocliente_TextChanged(object sender, EventArgs e)
    {
        if (txtvalordocliente.Text == "")
        {
            txtvalordocliente.Text = "0";
            decimal troco = decimal.Parse(txtvalordocliente.Text.Replace("R$", "")) - decimal.Parse(txtvalorareceber.Text.Replace("R$", ""));
            txttroco.Text = troco.ToString();
        }
        else
        {
            decimal troco = decimal.Parse(txtvalordocliente.Text.Replace("R$", "")) - decimal.Parse(txtvalorareceber.Text.Replace("R$", ""));
            txttroco.Text = troco.ToString();
        }
    }

And here’s my html code, but it either works once in a while or it doesn’t work, I can’t fix it.

 <asp:UpdatePanel ID="UpdatePanel12" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
                                <ContentTemplate>
                                    <asp:Label ID="Label44" runat="server" Text="Forma de Pagamento"></asp:Label>
                                    <br />
                                    <asp:DropDownList ID="cbformapg" runat="server">
                                        <asp:ListItem>DINHEIRO</asp:ListItem>
                                        <asp:ListItem>CHEQUE</asp:ListItem>
                                        <asp:ListItem>CARTÃO</asp:ListItem>
                                        <asp:ListItem>BOLETO</asp:ListItem>
                                        <asp:ListItem>DEPÓSITO</asp:ListItem>
                                    </asp:DropDownList>
                                    <br />
                                    <asp:Label ID="Label46" runat="server" Text="Valor Do Cliente"></asp:Label>
                                    <br />
                                    <asp:TextBox ID="txtvalordocliente" runat="server" onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$" AutoPostBack="True" OnTextChanged="txtvalordocliente_TextChanged"></asp:TextBox>
                                    <br />
                                    <asp:Label ID="Label47" runat="server" Text="Valor a Receber"></asp:Label>
                                    <br />
                                    <asp:TextBox ID="txtvalorareceber" runat="server"  onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$" AutoPostBack="False" OnTextChanged="txtvalorareceber_TextChanged"></asp:TextBox>
                                    <br />
                                    <asp:Label ID="Label48" runat="server" Text="Troco"></asp:Label>
                                    <br />
                                    <asp:TextBox ID="txttroco" runat="server"  onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$" AutoPostBack="True"></asp:TextBox>
                                    <br />
                                </ContentTemplate>
                            </asp:UpdatePanel>

Load event:

 if (!IsPostBack)
            {
                Session["contador"] = 0;
                CarregaTaxa();
                CarregaContas();
                btnReceber.Visible = false;
                btnExcluir.Visible = false;
                CarregaReceitas();
                idconta();
                CarregaServicos();
                txtObs.InnerText = cbreceita.SelectedItem + "(" + cbplanos.SelectedItem + ")" + " - " + txtPessoa.Text;
                txtValorIncluir.Enabled = true;
                Valor();
                txtVencimentoIncluir.Text = DateTime.Now.ToString("dd/MM/yyyy");
                DateTime data = DateTime.Now;
                Data.Value = data.ToString();
                SqlConnection conexao = clsdb.AbreBanco();
                SqlDataAdapter da = new SqlDataAdapter("select c.id,case WHEN s.descricao is null THEN 'VENDA DE PRODUTO'else s.descricao END AS Modalidade, convert(date,c.vencimento,103) as Vencimento,c.Observacao , c.valor_pago, c.valor, quitado, p.nome, p.id from dbo.contas_receber as c left join servicos as s  on s.id = c.servico_id left join pessoa as p on p.id = c.pessoa_id where vencimento >= (convert (char(10), getdate(),103)) and vencimento <= (convert (char(10), getdate(),103))  order by vencimento asc", conexao);

                ds = new DataSet();
                da.Fill(ds, "contas_receber");

                GridView1.DataSource = ds.Tables["contas_receber"];
                GridView1.DataBind();
                cbPesquisa.Text = "Pessoa";
                if (cbPesquisa.Text == "Pessoa")
                {
                    txtDataInicial.Visible = false;
                    txtDataFinal.Visible = false;
                    txtPesquisa.Visible = true;
                }
                if (cbPesquisa.Text == "Plano")
                {
                    txtDataInicial.Visible = true;
                    txtDataFinal.Visible = true;
                    txtPesquisa.Visible = true;
                }
                if (cbPesquisa.Text == "Vencimento")
                {
                    txtDataInicial.Visible = true;
                    txtDataFinal.Visible = true;
                    txtPesquisa.Visible = false;
                }

                if (cbPesquisa.Text == "Recebimento")
                {
                    txtDataInicial.Visible = true;
                    txtDataFinal.Visible = true;
                    txtPesquisa.Visible = false;
                }

                GridPessoa();
                GridComprador();
                CarregaProdutos();
                if (cbreceita.SelectedItem.ToString() != "VENDA DE PRODUTOS")
                {
                    idconta();
                    CarregaServicos();
                    txtObs.InnerText = cbreceita.SelectedItem + "(" + cbplanos.SelectedItem + ")" + " - " + txtPessoa.Text;
                    txtValorIncluir.Enabled = true;
                    Valor();
                    Button4.Visible = false;
                    Label22.Visible = true;
                    cbplanos.Visible = true;
                    txtValorIncluir.Enabled = true;
                    Button3.Enabled = true;
                }
                if (cbreceita.SelectedItem.ToString() == "VENDA DE PRODUTOS")
                {
                    txtObs.InnerText = cbreceita.SelectedItem + "(" + cbplanos.SelectedItem + ")" + " - " + txtPessoa.Text;
                    cbplanos.Visible = false;
                    txtValorIncluir.Enabled = false;
                    Button3.Enabled = false;
                    Button4.Visible = true;
                    Label22.Visible = false;
                }
                UpQuantidade.Text = "1";
                txtDescontoAdd.Text = "0,00";
                CarregaValorProduto();
                SqlConnection conConexao6 = clsdb.AbreBanco();
                SqlCommand cmd6 = new SqlCommand("SELECT valor_venda,id FROM produto where nome='" + cbProdutoAdd.SelectedItem + "'", conConexao6);
                SqlDataReader dr6 = cmd6.ExecuteReader();

                if (dr6.HasRows == true)
                {
                    if (dr6.Read())
                    {
                        txtValorAdd.Text = dr6[0].ToString();
                        txtValorFinalAdd.Text = dr6[0].ToString();
                        txtidprodutoAdd.Text = (dr6[1].ToString());
                    }
                }        
            }

  • Add the code of your entire event and also the page load.

  • I added the event, on the page load, has not related to this code, until I tried to put inside the if (!Ispostback) but it did not work.

  • Add pageLoad code, because your problem might be there.

  • I added Aline.

  • Actually Aline, rs now is not even updating, I tried to put updatepanel as always, autopostback as true of all, but no way is working now.

  • Wait, at what point do you want the value to appear in txttroco? Pq Voce set Autopostback to false in txtvalorareceber..

  • I wanted that when typed in txtvalordoclient txttroco was updated, the field txtvalorareceber, it comes filled automatically.

  • Hummm, so if you don’t want to give the postback, you’ll have to do with js updating that field.

  • That’s why I use Updatepanel, to update only the textbox, but it’s not working, I use it in other contexts and it works.

Show 4 more comments

2 answers

1


Since you do not want the postback of the page to update the #txtTroco, a suggestion is to do this process in the client, with js.

You will need to change your html code so that the element Ids do not change (Clientidmode):

<asp:UpdatePanel ID="UpdatePanel12" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:Label ID="Label44" runat="server" Text="Forma de Pagamento">/asp:Label>
    <asp:DropDownList ID="cbformapg" runat="server">
        <asp:ListItem>DINHEIRO</asp:ListItem>
        <asp:ListItem>CHEQUE</asp:ListItem>
        <asp:ListItem>CARTÃO</asp:ListItem>
        <asp:ListItem>BOLETO</asp:ListItem>
         <asp:ListItem>DEPÓSITO</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label46" runat="server" Text="Valor Do Cliente"></asp:Label>
    <asp:TextBox ID="txtvalordocliente" runat="server" ClientIDMode="Static" onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$" onblur />                                        
    <asp:Label ID="Label47" runat="server" Text="Valor a Receber"></asp:Label>
    <asp:TextBox ID="txtvalorareceber" runat="server"  onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$"/>
    <asp:Label ID="Label48" runat="server" Text="Troco"></asp:Label>
    <asp:TextBox ID="txttroco" runat="server" ClientIDMode="Static" onKeyPress="return(MascaraMoeda(this,'.',',',event))" placeholder="R$"/>
</ContentTemplate>
</asp:UpdatePanel>

and write a js, I will use jquery:

<script>
    $(document).ready(function(){
        $(document).on("blur", "#txttroco", function(){
            var valorcliente = $("#txtvalordocliente").val();
            if(!valorcliente){
                valorcliente = 0;
            $("#txtroco").val(Number(valorcliente.replace("R$", "")) - Number($("#txtvalorareceber").val().replace("R$", "")));
        });
    });
    </script>

1

The problem may be in the understanding of property Childrenastriggers.

If the property is

Childrenastriggers="false"

it is necessary to inform Triggers.

So you have 2 options or place the property

Childrenastriggers = true (default)

Or add Triggers, as in the example below:

 <asp:UpdatePanel ID="UpdatePanel12" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label44" runat="server" Text="Forma de Pagamento"></asp:Label>
            <br />
            <asp:DropDownList ID="cbformapg" runat="server">
                <asp:ListItem>DINHEIRO</asp:ListItem>
                <asp:ListItem>CHEQUE</asp:ListItem>
                <asp:ListItem>CARTÃO</asp:ListItem>
                <asp:ListItem>BOLETO</asp:ListItem>
                <asp:ListItem>DEPÓSITO</asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:Label ID="Label46" runat="server" Text="Valor Do Cliente"></asp:Label>
            <br />
            <asp:TextBox ID="txtvalordocliente" runat="server" AutoPostBack="true" placeholder="R$" Text="0" OnTextChanged="txtvalordocliente_TextChanged"></asp:TextBox>
            <br />
            <asp:Label ID="Label47" runat="server" Text="Valor a Receber"></asp:Label>
            <br />
            <asp:TextBox ID="txtvalorareceber" runat="server" Text="500"></asp:TextBox>
            <br />
            <asp:Label ID="Label48" runat="server" Text="Troco"></asp:Label>
            <br />
            <asp:TextBox ID="txttroco" runat="server"></asp:TextBox>
            <br />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="txtvalordocliente" EventName="TextChanged" />
        </Triggers>
    </asp:UpdatePanel>

Codebehind

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                txtvalorareceber.Text = "550";
        }
 protected void txtvalordocliente_TextChanged(object sender, EventArgs e)
        {
            decimal valorcliente = 0;
            decimal valorreceber = 0;
            decimal troco = 0;

            if (Decimal.TryParse(txtvalordocliente.Text, out valorcliente) && Decimal.TryParse(txtvalorareceber.Text, out valorreceber))
                troco = valorcliente - valorreceber;

            txttroco.Text = troco.ToString();
        }

Browser other questions tagged

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