按钮阵列上的单击事件 [英] Click events on Array of buttons

查看:18
本文介绍了按钮阵列上的单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取上次在面板上单击的对象的名称?诀窍是面板上有大量按钮(btn[1] ... btn [200]).如何检查我是否点击了按钮 b[180] 或 b[11] 甚至面板外(无按钮)?此外,按钮是通过编码在页面加载时生成的.谢谢你.安娜

谢谢!出现的另一个问题(这生成了一个 NULL 对象引用):我有一个与 buttonHandler() 处于同一级别的方法,它被命名为 HowManyClicked() 并且它是从 buttonHandler() 内部调用的.在 HowManyClicked() 中,我想将 Button btn1 = Panel2.FindControl(x) 标识为 Button;例如,其中 x 是 buttonArray[2,3].但我总是得到NULL.按钮数组 buttonArray 是不是在生成它的方法之外无法通过名称识别??

 public void buttonHandler(object sender, EventArgs e){按钮 btn = 发送者为按钮;//string tt = btn.ToolTip.ToString();btn.BackColor = Color.Red;statusL.Text = HowManyClicked().ToString();}public int HowManyClicked(){int sum=0;for (int a = 0; a <10; a++)for (int b = 0; b <14; b++){string x = "buttonArray[" + a + ", " + b + "]";statusL.Text = x;Button btn1 = Panel2.FindControl(x) as Button;if (btn1.BackColor == Color.Red) sum += 1;}返还金额;}

解决方案

正如@AVD 所评论的,你可以得到发起回发的按钮,投射发送者对象,你也可以使用 CommandNameCommandArgument 来自按钮对象的属性(它们通常在按钮位于 GridDataList 等内部时使用,但如果您需要):

 protected void Page_Init(object sender, EventArgs e){var s = Enumerable.Range(1, 10);foreach(s 中的 var 项目){var b = new Button();b.Text = "我的按钮" + item.ToString();b.CommandName = "自定义命令";b.CommandArgument = item.ToString();b.Click += new EventHandler(b_Click);this.myPanel.Controls.Add(b);}}void b_Click(对象发送者,EventArgs e){var current = sender as Button;this.lblMessage2.Text = "从数组按钮中点击:<br/>命令名称:" + current.CommandName + "<br/>Args:" + current.CommandArgument + "<br/>Button唯一 ID:" + current.UniqueID + "<br/>客户端 ID:" + current.ClientID;}

页面:

已编辑

 protected void Page_Load(object sender, EventArgs e){如果 (!this.IsPostBack){this.ViewState["count"] = 0;}}protected void Page_Init(对象发送者,EventArgs e){var s = Enumerable.Range(1, 10);foreach(s 中的 var 项目){var b = new Button();b.Text = "我的按钮" + item.ToString();b.Click += new EventHandler(buttonHandler);this.myPanel.Controls.Add(b);}}void buttonHandler(object sender, EventArgs e){//在这里更新你的控件var b = 发件人为按钮;b.BackColor = Color.Red;HowManyClicked();}私有无效 HowManyClicked(){var c = (int)this.ViewState["count"];C++;this.ViewState["count"] = c;this.lblMessage2.Text = "点击控件:" + this.ViewState["count"].ToString();}

这产生了:

How can I get the name of the object last clicked on a panel? The trick is there is a big array of buttons on the panel (btn[1] ... btn [200]). How can I check if I clicked on button b[180], or b[11] or even outside the panel (no button)? Also the buttons are generated at page load via coding. Thank you. Anna

EDIT: Thank you! Another issue that arose (this generated a NULL object reference): I have a method on the same level as buttonHandler(), it is named HowManyClicked() and it's called from within buttonHandler(). Inside HowManyClicked() I want to identify Button btn1 = Panel2.FindControl(x) as Button; where x is, for example, buttonArray[2,3]. But I always get NULL. Is the button array buttonArray not identifiable by name once out of the method that generated it??

        public void buttonHandler(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            //string tt = btn.ToolTip.ToString();
            btn.BackColor = Color.Red;
            statusL.Text = HowManyClicked().ToString();

        }

        public int HowManyClicked()
        {
            int sum=0;
            for (int a = 0; a < 10; a++)
                for (int b = 0; b < 14; b++)
                {
                    string x = "buttonArray[" + a + ", " + b + "]";
                    statusL.Text = x;
                    Button btn1 = Panel2.FindControl(x) as Button;
                    if (btn1.BackColor == Color.Red) sum += 1;

                }
            return sum;
        }

解决方案

As @AVD commented you can get the button originating the postback casting the sender object, you can also use the CommandName and CommandArgument properties from the button object (they are usually used when the button is inside a Grid, DataList etc but you can use them in this context if you need):

    protected void Page_Init(object sender, EventArgs e)
    {
        var s = Enumerable.Range(1, 10);
        foreach (var item in s)
        {
            var b = new Button();
            b.Text = "My Button " + item.ToString();
            b.CommandName = "custom command";
            b.CommandArgument = item.ToString();
            b.Click += new EventHandler(b_Click);
            this.myPanel.Controls.Add(b);
        }
    }

    void b_Click(object sender, EventArgs e)
    {
        var current = sender as Button;
        this.lblMessage2.Text = "Clicked from array buttons: <br/>Command Name: " + current.CommandName + "<br/>Args: " + current.CommandArgument + "<br/>Button Unique ID: " + current.UniqueID + "<br/>Client ID: " + current.ClientID;
    }

Page:

<asp:Panel runat="server" ID="myPanel">
</asp:Panel>

<asp:Label ID="lblMessage2" runat="server" />

This code generates something like:

As an additional note, Microsoft recommends to create dynamic controls in the PreInit event or in case you are using a master page, in the Init event

source

Edited

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.ViewState["count"] = 0;
        }

    }


    protected void Page_Init(object sender, EventArgs e)
    {
        var s = Enumerable.Range(1, 10);
        foreach (var item in s)
        {
            var b = new Button();
            b.Text = "My Button " + item.ToString();
            b.Click += new EventHandler(buttonHandler);
            this.myPanel.Controls.Add(b);
        }
    }

    void buttonHandler(object sender, EventArgs e)
    {
        // update here your control
        var b = sender as Button;
        b.BackColor = Color.Red;
        HowManyClicked();
    }

    private void HowManyClicked()
    {
        var c = (int)this.ViewState["count"];
        c++;
        this.ViewState["count"] = c;
        this.lblMessage2.Text = "Clicked controls: " + this.ViewState["count"].ToString();
    }

This produced:

这篇关于按钮阵列上的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆