Calling event from within code

Asked

Viewed 2,429 times

1

I have this event in my code:

protected void rdbGarantiaConfissao_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Declarações
            RadioButtonList vrblAprovado = sender as RadioButtonList;
            try
            {
                //Instâncias e Inicializações

                //Desenvolvimento
                if (vrblAprovado.SelectedValue == "1")
                    MostraConfissaoGarantia(1);
                else
                    MostraConfissaoGarantia(2);
            }
            catch (Exception Ex)
            {
                Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
                Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            }
        }

How do I stop at another part of the code I run that event?

I just want to avoid it:

if (rdbGarantiaConfissao.SelectedValue == "1")
   wucCadastroConfissaoDividaPV.Visible = true;
else
   wucGarantiaAdicionalPV.Visible = true;

Because the Selectedindexchanged event already does so, as can be seen.

  • What other part of the code? Be specific.

  • Call the method the way you do with any other. Pass one RadioButtonList. The parameter EventArgs may be null.

  • I want to call the Selectedindex of my Radiobuttolist, another method.

  • Within this same class? You need to know exactly who will call you to call the right way (and see if you can call).

  • The Sender is your Radiobuttolist

  • But who is the Radiobuttolist? If the method is protected you can only call within this class or one that inherits from this.

  • be more specific, where you want to call this method, this Radiobuttolist has onchange method ?

  • @pnet, the call will be made from the same Class (Page or Usercontrol) or from another Page or Usercontrol?

  • @bigown in this case the Radiobuttolist is this

  • @That’s what I think but I’m not sure.

  • Everything is in the same class.

Show 6 more comments

3 answers

1

Thus resolved:

protected void rdbGarantiaConfissao_SelectedIndexChanged(object sender, EventArgs e)
        {
           NomeMetodo(sender);
        }

private void NomeMetodo(object sender){
 //Declarações
            RadioButtonList vrblAprovado = sender as RadioButtonList;
            try
            {
                //Instâncias e Inicializações

                //Desenvolvimento
                if (vrblAprovado.SelectedValue == "1")
                    MostraConfissaoGarantia(1);
                else
                    MostraConfissaoGarantia(2);
            }
            catch (Exception Ex)
            {
                Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
                Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            }
}

protected void NomeEvento2(){
   NomeMetodo(rdbGarantiaConfissao);
}
  • 1

    I don’t understand why you don’t just use: rdbGarantiaConfissao_SelectedIndexChanged(this, null);

  • I didn’t think about it. This would be my Sernder, right?

  • 1

    Yes. Of course this is only valid because the call is made in the class itself, nor would it be possible otherwise since the method is protected

0

  • 3

    Are you sure this answers the question?

  • No, actually the event works. So much so that it works, that I now want to run it off, just not to stay doing if with it. I’ve done it once before in another company and I can’t remember how it was done.

0

If you intend to use only the property SelectedValue of RadioButtonList, you can create a method that takes a parameter of type String, something like that:

protected void MeuMetodo(String selectedValue) 
{
    try
    {
        //Instâncias e Inicializações

        //Desenvolvimento
        if (selectedValue == "1")
            MostraConfissaoGarantia(1);
        else
            MostraConfissaoGarantia(2);
    }
    catch (Exception Ex)
    {
        Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
        Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
    }
}

To use this method in the event SelectedIndexChanged or in any other part of the code, you can do something like this:

protected void rdbGarantiaConfissao_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList vrblAprovado = sender as RadioButtonList;
    if (vrblAprovado != null)
        MeuMetodo(vrblAprovado.SelectedValue);
    else
        // o que você irá fazer se sender não for do tipo RadioButtonList
}

protected void QualquerOutroMetodoQueVoceTem()
{
    MeuMetodo(SeuRadioButtonList.SelectedValue);
}

If you really need to work with more information that is on RadioButtonList, can do so:

protected void MeuMetodo(RadioButtonList radioButtonList) 
{
    try
    {
        //Instâncias e Inicializações

        //Desenvolvimento
        if (radioButtonList.SelectedValue == "1")
            MostraConfissaoGarantia(1);
        else
            MostraConfissaoGarantia(2);
    }
    catch (Exception Ex)
    {
        Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
        Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
    }
}

And to call him:

protected void rdbGarantiaConfissao_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList vrblAprovado = sender as RadioButtonList;
    if (vrblAprovado != null)
        MeuMetodo(vrblAprovado);
    else
        // o que você irá fazer se sender não for do tipo RadioButtonList
}

protected void QualquerOutroMetodoQueVoceTem()
{
    MeuMetodo(SeuRadioButtonList);
}

Note that the "treatment" of exceptions is done within the MeuMetodo, here at Sopt have some questions dealing with this subject.

Browser other questions tagged

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