Call variable of a click event

0

Is it possible to call the variable of a click event, in another click event?

If possible ... How do I do it?

Vera, I have created an event and within this I have made buttons dynamically, I have assigned the same event to them to do the same process and bring different values with respect to their name.

What I want to try is the following:

This is where I create the buttons and assign them an event.

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

            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 == name && 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.Click += new EventHandler(Info_Click);

                newButton.OnClientClick = "return ModalGood();";
                Botones.Add(newButton);

                GoodPanel.Controls.Add(newButton);
                newButton.CssClass = "btn-primary outline separate";
            }
        }

And here is where I want to call the variable name previously declared to fill the GridView

protected void Info_Click(object sender, EventArgs e)
        {
            Button Btnclick = (Button)sender;
            var team = Btnclick.Text;

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

            List<Data.Area> ListOfToolsOk = Tools.Where(x => x.AREA == name && x.TEAM == team && x.STANDBY == 0).ToList();
                                                                        ^
                                                                        |
                                                                Variable a llamar


            //var ToolArea = ListOfToolsOk.Select(x => x.TEAM)
            Grv_Eng.DataSource = ListOfToolsOk;
            Grv_Eng.DataBind();
        }
    
asked by Cesar Gutierrez Davalos 30.06.2017 в 21:26
source

2 answers

1

Use a hiddenfield:

<asp:HiddenField ID="HiddenField1" runat="server" />

To which you assign a value in the value property:

Hiddenfield1.value = Btn_clic.Text;

You can also use session variables:

session["variable"]= Btn_clic.Text;
string strBtn = (string)session["variable"];

With session variables consider that after a while, they expire and lose the stored value.

    
answered by 04.07.2017 / 00:29
source
0

What you are trying to do is not possible since the variable is created locally in the click event of the first button and you can only use it in that area. If you want to store data associated with a button it is better to use the tag property that lets you store data of type object.

    
answered by 30.06.2017 в 22:26