How to pass parameters to an event that you do not receive?

0

I am trying to pass the objeto sender to an event that does not receive parameters, I need the sender object in order to generate the information

This is the event sent by the sender object:

protected void DButton(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);

            enable = true;
            DynamicButton();
        }

and this is the event I want the sender object to receive

protected void DynamicButton()
        {
            Button Btn_clic = (Button)sender;
            var name = Btn_clic.Text;

            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == NECESITO EL EVENTO PARA CAMBIAR ESTE DATO ---> "ENG" && x.STANDBY == 0).ToList();

            List<Button> Botones = new List<Button>();

            var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
            foreach (var team in TeamFCH)
            {

                Button newButton = new Button();
                newButton.CommandName = "Btn" + Convert.ToString(team);
                newButton.ID = "Btn_" + Convert.ToString(team);
                newButton.Text = team;
                newButton.CommandArgument = "ENG";

                newButton.Click += new EventHandler(newButton_Click);

                pan1.Controls.Add(newButton);
                Botones = pan1.Controls.OfType<Button>().ToList();
                Botones.Add(newButton);

                newButton.CssClass = "btn-primary outline separate";

            }            
        }

and that's how everything starts

public partial class Dashboard : System.Web.UI.Page
    {
        static bool enable = false;

        protected void Page_Load(object sender, EventArgs e)
        {   
            if (!IsPostBack)
            {
                DynamicButton();
            }
            else if(enable)
            {
                DynamicButton();
            }    
        }
    
asked by Cesar Gutierrez Davalos 06.07.2017 в 17:41
source

2 answers

1

I still do not understand why you do not make the DynamicButton method receive the sender parameter.

void DynamicButton(object sender);
  

@EinerSantana if I do it that way it marks me wrong in many ways   and the DynamicButton event does not start

Obviously not because you have to pass the sender parameter. If the method depends on that parameter then pass it in all the places that require it.

But if you do not want to send the sender then pass the text that you expect to generate the botton dynamically. Since you only need the sender to get the property Text :

protected void DynamicButton(string nombre = null)
{
   if(String.IsNullOrEmpty(nombre)) return;

   var name = nombre;
   //..
}

//..
DynamicButton("NombreButton");

The condition what it does is to avoid that if you send a name empty then it does not generate the Button . And adding the optional parameter would not give you an error everywhere as you mentioned.

    
answered by 07.07.2017 / 14:52
source
0

in .NET there is a convention that events are based on this delegate

public delegate void EventHandler(object sender, EventArgs e);  

As you can see, the DynamicButton event that you are using does not comply with said convention, simply change the method so that it accepts the required parameters.

EDITING

Before I forget, it also exists:

 EventHandler<TEventArgs>

but it is not according to the CLS so I recommend using the one above

    
answered by 06.07.2017 в 17:56