How to know which button called the page?

Asked

Viewed 275 times

1

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

What I did was put them to add a value to a Hidden field to find out 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";
}
  • Add to your question the code surrounding the 3 buttons and what exactly they do!

  • Can be passed as querystring, and on the page called would have to pick up this value on pageload

  • If you want to get the value on another page, why not save the value on a Session?

2 answers

2

it is possible to use the sender to perform the identification by converting the object to a button and using the . property.

Button BotaoClicado = sender as Button;
        string Nome = BotaoClicado.Name;

0

Hello, if I understand correctly, you are redirecting from one page to another after pressing any 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.

Browser other questions tagged

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