C#如何将不同按钮的按钮文本设置为同一文本框

时间:2017-09-26 14:49:21

标签: c# asp.net button textbox

我有几个按钮和一个文本框。我想这样做,当我按下按钮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;
    }

修改:有更短的方法吗?

1 个答案:

答案 0 :(得分:7)

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

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

您可以通过单击按钮,转到“属性”窗口并单击事件的小闪电符号并选择Button_Click方法来更改设计器中用于按钮事件的方法。 {1}}事件。