我是C#的新手已经慢慢学习了几个星期,我知道一些基本的JavaScript。
我不想让三个按钮将字符串或int的值更改为不同按钮处的不同值,然后启动所有按钮共享的公共事件。我知道我可以复制并粘贴它们,但我想缩短代码。
protected void btn1_Click(object sender, EventArgs e)
{
string example = a;
//Start the MainEvent
}
protected void btn2_Click(object sender, EventArgs e)
{
string example = b;
//Start the MainEvent
}
protected void btn3_Click(object sender, EventArgs e)
{
string example = c;
//Start the MainEvent
}
protected void MainEvent(object sender, EventArgs e)
{
//Content of MainEvent. Result of MainEvent is determined by the value of "Example".
}
答案 0 :(得分:2)
您只能使用一个事件处理程序来执行此操作:
btn1.Click += MainEvent;
btn2.Click += MainEvent;
btn3.Click += MainEvent;
protected void MainEvent(object sender, EventArgs e)
{
string example;
if(sender == btn1)
{
example = a
}
else if(sender == btn2)
{
example = b
}
else if(sender == btn3)
{
example = c
}
//Do whatever with example
}