【问题标题】:C# How to set button text of different buttons to the same textboxC#如何将不同按钮的按钮文本设置为同一个文本框
【发布时间】:2018-03-07 20:47:49
【问题描述】:

我有几个按钮和一个文本框。我想这样做,当我按下按钮 1 时,文本框中的文本转到按钮,当我按下按钮 2 时,文本转到按钮 2,依此类推。我现在有这个:

protected void Button1_Click(object sender, EventArgs e)
    {

        Button1.Text = TextBox1.Text;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Button2.Text = TextBox1.Text;
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Button3.Text = TextBox1.Text;
    }

编辑:有更短的方法吗?

【问题讨论】:

  • ((Button)sender).Text = TextBox1.Text;
  • 你需要按下按钮吗?
  • 是的,当我单击按钮时,我希望文本框中的文本继续显示在按钮上。
  • 那么@EdPlunkett 的评论就是你的解决方案

标签: c# asp.net button textbox


【解决方案1】:

如果您将每个按钮的Click 事件指向相同的方法,您可以在这样的一种方法中使用它;

protected void Button_Click(object sender, EventArgs e)
{
    ((Button)sender).Text = TextBox1.Text;
}

您可以在设计器中更改用于按钮事件的方法,方法是单击按钮,转到“属性”窗口并单击事件的小闪电符号,然后为Click 选择Button_Click 方法事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2020-01-19
    • 1970-01-01
    • 2012-06-16
    • 2011-05-09
    • 2018-10-28
    • 1970-01-01
    相关资源
    最近更新 更多