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);
}
}
... {
// 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事件中调用。