解决方法
如果对于不同的按钮使用相同的EventHandler方法,这可能很有用。例如,说你的标记如下所示:
<asp:Button ID="button1" runat="server" CommandArgument="MyVal1" CommandName="ThisBtnClick" OnClick="MyBtnHandler" /> <asp:Button ID="button2" runat="server" CommandArgument="MyVal2" CommandName="ThatBtnClick" OnClick="MyBtnHandler" />
您可以为两个按钮拥有相同的事件处理程序,并根据CommandName进行区分:
protected void MyBtnHandler(Object sender,EventArgs e) { Button btn = (Button)sender; switch (btn.CommandName) { case "ThisBtnClick": DoWhatever(btn.CommandArgument.ToString()); break; case "ThatBtnClick": DoSomethingElse(btn.CommandArgument.ToString()); break; } }