事件源转换成button按钮进行相关处理.

前台代码:

 

代码
  <div>
        
<asp:Button ID="btn1" runat="server" Text="张三" CommandName="1" 
            onclick
="btn1_Click" />
        
<asp:Button ID="btn2" runat="server" Text="李四" CommandName="2" 
            onclick
="btn1_Click" />
        
<asp:Button ID="btn3" runat="server" Text="王五" CommandName="3" 
            onclick
="btn1_Click" />
    
</div>

 

 

 

后置代码:

 

代码
  protected void btn1_Click(object sender, EventArgs e)
        {
            Button btn 
= (Button)sender;//事件源对象转为Button
            string str=btn.CommandName;
            
switch (str)
            {
                
case "1":
                    Response.Write(
"张三");
                    
break;
                
case "2":
                    Response.Write(
"李四");
                    
break;
                
case "3":
                    Response.Write(
"王五");
                    
break;
                
default:
                    
break;
            }
        }

 

 

posted @ 2010-05-23 23:55  唔愛吃蘋果  阅读(239)  评论(0编辑  收藏  举报