在textbox中输入值后,单击回车,指定调用哪一个按钮的方法

public   static   void  TieButton(Page page, Control TextBoxToTie, Control ButtonToTie)
  
{
    
// Init jscript
    string jsString = "";

    
// Check button type and get required jscript
    if (ButtonToTie is LinkButton)
    
{
      jsString 
= "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
          
+ page.ClientScript.GetPostBackEventReference(ButtonToTie, "").Replace(":""$"+ ";return false;} else return true;";
    }

    
else if (ButtonToTie is ImageButton)
    
{
      jsString 
= "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
          
+ page.ClientScript.GetPostBackEventReference(ButtonToTie, "").Replace(":""$"+ ";return false;} else return true;";
    }

    
else
    
{
      jsString 
= "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."
          
+ "forms[0].elements['" + ButtonToTie.UniqueID.Replace(":""_"+ "'].click();return false;} else return true; ";
    }


    
// Attach jscript to the onkeydown attribute - we have to cater for HtmlControl or WebControl
    if (TextBoxToTie is HtmlControl)
    
{
      ((HtmlControl)TextBoxToTie).Attributes.Add(
"onkeydown", jsString);
    }

    
else if (TextBoxToTie is WebControl)
    
{
      ((WebControl)TextBoxToTie).Attributes.Add(
"onkeydown", jsString);
    }

  }

 此方法可以置于Utilities类中,在Page_Load事件中调用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值