将“ OnClick”事件添加到按钮后面的代码中

问题描述 投票:0回答:1

因此,我创建了一个按钮,我想通过单击它来更改其文本。

这是我的代码背后的内容(来自Page_Load代码):

Button InviteToGameBTN = new Button();
InviteToGameBTN.Click += new EventHandler(InviteToGameBTN_OnClick);

protected void InviteToGameBTN_OnClick(object sender,EventArgs e)
{
        Button b1 = (Button)sender;
        b1.Text = "Text changed";

}

可能出什么事了?谢谢大家。

c# asp.net button onclick
1个回答
0
投票

尝试一下:


public class Foo
{
    public Foo()
    {
        Button myButton = new Button();
        myButton.Content = "Old text";
        myButton.Click += ButtonClicked;
        yourGrid.Children.Add(myButton);
    }

    private void ButtonClicked(object sender, RoutedEventArgs e)
    {
        Button clickedButton = (Button) sender;
        clickedButton.Content = "New text";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.