这是一个比较通用的方法,可以用在网站的很多地方,建议大家Copy回去,欢迎大家提供更好的方法
public static void TieButton(Page page, Control TextBoxToTie,
Control ButtonToTie)
{
// 初始化Jscript,实现原理是向客户端发送特定Jscript
// ps.这种方法在其他地方也用得很多
string jsString = "";
// 检查输入框对应的事件按纽
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; ";
}
// 把 jscript 附加到输入框的onkeydown属性
if (TextBoxToTie is HtmlControl)
{
((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
else if (TextBoxToTie is WebControl)
{
((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
}