如何获取对象发件人?

时间:2017-07-06 13:32:49

标签: c# asp.net webforms

当我点击按钮时,我正在尝试发送object sender

以前我的事件点击不会触发,但现在它正在制作我的代码:

 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();
            }    
        }

这是更改DynamicButton并启动所有内容的事件:

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

            enable = true;
            DynamicButton();
        }

这是我开始创建动态按钮的地方:

protected void DynamicButton()
        {
            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == "ENG" && x.STANDBY == 0).ToList();

            var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();

            foreach (var team in TeamFCH)
            {
                LinkButton newButton = new LinkButton();
                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);

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

            }
        }

这里是创建的按钮开始他的活动的地方

protected void newButton_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == "ENG" && x.TEAM == "516" && x.STANDBY == 0).ToList();

            var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
            Grv_Eng.DataSource = ListOfToolsOk;
            Grv_Eng.DataBind();
        }

我的问题是

如何将sender的{​​{1}}事件发送到DynamicButton进行更改?

我的意思是:

DButton

我需要Button Btn_clic = (Button)sender; var name = Btn_clic.Text; 的事件发件人来更改此

DButton

List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == CHANGE THIS -> "ENG" && x.STANDBY == 0).ToList();

来自newButton.CommandArgument = CHANGE THIS -> "ENG";

如何创建动态按钮的事件DynamicButton,如何从DButton获取事件以发送到sender

我的意思是

NewButton_Click

并更改来自Button Btnclick = (Button)sender; var team = Btnclick.Text; string name = Btnclick.CommandArgument;

newButton_Click

1 个答案:

答案 0 :(得分:1)

我还在处理你的问题。但我确实让你的活动奏效了。如果这有帮助,请告诉我,我会继续提供帮助。在newButton_Click的开头放置一个断点,看它现在在这里打破了。到目前为止对互联网邮件的信用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace List
{
    public class ListUsers
    {
        public List<Data.Area> AreaList()
        {
            var list = new List<Data.Area>();
            Data.Area area1 = new Data.Area { AREA = "ENG", STANDBY = 0, TEAM = "team1" };
            Data.Area area2 = new Data.Area { AREA = "ENG", STANDBY = 0, TEAM = "team2" };
            Data.Area area3 = new Data.Area { AREA = "area3", STANDBY = 3, TEAM = "team3" };
            list.Add(area1);
            list.Add(area2);
            list.Add(area3);
            return list;
        }
    }
}

namespace Data
{
    public class Area
    {
        public string AREA { get; set; }
        public int STANDBY { get; set; }
        public string TEAM { get; set; }
    }
}

namespace WebApplication1
{
    public partial class Dashboard : System.Web.UI.Page
    {
        //BTW static variable are shared across multiple users of your web site
        static bool enable = false;

        override protected void OnInit(EventArgs e)
        {
            //NEED TO set event listener in oninit everytime
            DynamicButton();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //DynamicButton();

            }
            else if (enable)
            {
                //DynamicButton();
            }
        }

        protected void DynamicButton()
        {
            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == "ENG" && x.STANDBY == 0).ToList();

            var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();

            foreach (var team in TeamFCH)
            {
                LinkButton newButton = new LinkButton();
                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);

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

        public void newButton_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == "ENG" && x.TEAM == "516" && x.STANDBY == 0).ToList();

            var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
            Grv_Eng.DataSource = ListOfToolsOk;
            Grv_Eng.DataBind();
        }

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

            enable = true;
            DynamicButton();
        }
    }
}