Update Textbox data on different . ascx components, but on the same page . aspx

Asked

Viewed 37 times

-2

Once again in need of your help!

Next, I have a page CadastroProposta.aspx, 2 custom components are loaded inside this page, DadosPagamento.ascx and DadosCobranca.ascx.

In the component DadosPagamento.ascx, have a <asp:Button ID="btnConfirmar"> and a <asp:TextBox ID="txtNumeroPropostaOrigem" runat="server">.

In the component DadosCobranca.ascx, have a <asp:TextBox ID="txtNumeroPropostaDestino" runat="server">.

What I need is to click on Button Confirmar (DadosPagamento.ascx), the value that is in the TextBox de origem (DadosPagamento.ascx), appear in the TextBox de Destino(DadosCobranca.ascx).

I’ve researched a lot about Postback, but I couldn’t implement in my application.

If anyone can help me, I’d appreciate it!

  • See if this is what you were looking for. And edit your question by presenting the actual code of your controls, this makes it easier to understand the problem and to come up with an answer to your question

1 answer

0


In the origin control, you implement the event of delegate, post the message with the MessageHandler at the click of the button. The source control must have a public method to handle this message and you bind both to parent where controls were included, see the example below.

inserir a descrição da imagem aqui

Dadospagamento.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DadosPagamento.ascx.cs" Inherits="WebApplication.Forms.DadosPagamento" %>
<h1>Dados de Pagamento</h1>
<asp:TextBox ID="txtNumeroPropostaOrigem" runat="server" />
<asp:Button ID="btnConfirmar" runat="server" Text="Confirmar" OnClick="btnConfirmar_Click"/>

Dadospagamento.ascx.Cs

public delegate void MessageHandler(string codigoProposta);
public partial class DadosPagamento : System.Web.UI.UserControl
{
    public event MessageHandler EnviarProposta;
    protected void Page_Load(object sender, EventArgs e)
    {

    }        


    protected void btnConfirmar_Click(object sender, EventArgs e)
    {
        EnviarProposta(txtNumeroPropostaOrigem.Text);
        txtNumeroPropostaOrigem.Text = string.Empty;
    }
}

Dadoscobranca.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DadosCobranca.ascx.cs" Inherits="WebApplication.Forms.DadosCobranca" %>
<h1>Dados de Cobrança</h1>
<asp:TextBox ID="txtNumeroPropostaDestino" runat="server"></asp:TextBox>

Dadoscobranca.ascx.Cs

public partial class DadosCobranca : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void CarregarProposta(string codigoProposta)
    {
        txtNumeroPropostaDestino.Text = codigoProposta;
    }
}

Registration.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CadastroProposta.aspx.cs" Inherits="WebApplication.Forms.CadastroProposta" %>

<%@ Register Src="~/DadosPagamento.ascx" TagPrefix="ucPagamento" TagName="DadosPagamento" %>
<%@ Register Src="~/DadosCobranca.ascx" TagPrefix="ucCobranca" TagName="DadosCobranca" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <ucPagamento:DadosPagamento runat="server" id="DadosPagamento" />
        </div>
        <hr />
        <div>
            <ucCobranca:DadosCobranca runat="server" id="DadosCobranca" />
        </div>
    </form>
</body>
</html>

Cadastroproposta.aspx.Cs

public partial class CadastroProposta : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DadosPagamento.EnviarProposta += m => DadosCobranca.CarregarProposta(m);
    }
}
  • in the proposed registration.aspx it is mandatory to place the ascx components inside a form? Mine is clearing the Origin Textbox, but it’s not populating Destiny’s!

  • All Asp.net components need to be positioned within one <form runat="server"> for its proper functioning and there can only be one per page. Your user controls should not have another form. And if they’re not, you won’t be able to use Postback

  • @Renatodamazio managed to solve?

  • I managed to resolve yes. Thank you!!!

Browser other questions tagged

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