How to know which button called the page?

1

I have a page with 3 buttons that call the same page and wanted to know if you have how to see which of the 3 buttons called the page. These are Web Forms buttons.

What I did was put them to add a value in a hidden field to know on the other page who called.

protected void Button1_Click1(object sender, EventArgs e)
{
    hdfLivroEscolhido.Value = "3";
}

protected void btnLivro1_Click(object sender, EventArgs e)
{
    hdfLivroEscolhido.Value = "1";
}

protected void btnLivro2_Click(object sender, EventArgs e)
{
    hdfLivroEscolhido.Value = "2";
}
    
asked by anonymous 27.02.2017 / 19:17

2 answers

2

You can use sender to perform the identification by converting the object for a button and using the .Name property.

Button BotaoClicado = sender as Button;
        string Nome = BotaoClicado.Name;
    
01.03.2017 / 17:16
0

Hello, if I understood correctly, you are redirecting from one page to another after pressing one of the buttons, correct? Then try the following:

// Na página com seus botões
protected void Button1_Click1(object sender, EventArgs e)
{
    Response.Redirect("UrlDaSuaPagina?btnValue=3");
}

protected void btnLivro1_Click(object sender, EventArgs e)
{
    Response.Redirect("UrlDaSuaPagina?btnValue=1");
}

protected void btnLivro2_Click(object sender, EventArgs e)
{
    Response.Redirect("UrlDaSuaPagina?btnValue=2");
}

// E em sua página para qual foi redirecionado
public void Page_Load(object sender, EventArgs e)
{
    string botao = Request.QueryString["btnValue"];
    // apenas informativo
    minhaLabel.Text = botao;
    // sua lógica para o botão que foi apertado
}

With this you will be able to identify which button triggered the redirect to your page.

    
02.03.2017 / 13:35