In my form I have a text box into which the user enters a URL, and two buttons: upload and cancel.
I wanted the upload button to be activated when the user types return while in the text box.
To accomplish this, I followed advice found on a website for handling the enter key:
Code: C#
/*
* Causes a text box in a form to activate the designated button as its default button on a carriage return
* in a way that is supported by numerous browsers.
* See http://codebetter.com/blogs/darrell.norton/archive/2004/03/03/8374.aspx for commentary.
*/
public void SetDefaultButton(Page page, TextBox textControl, Button defaultButton)
{
// Sets default buttons.
// Originally created by Janus Kamp Hansen - http://www.kamp-hansen.dk
// Extended by Darrell Norton - http://dotnetjunkies.com/weblog/darrell.norton/
// -- added Mozilla support, fixed a few issues, improved performance
string theScript = @"
<SCRIPT language=""javascript"">
<!--
function fnTrapKD(btn, event){
if (document.all){
if (event.keyCode == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if (document.getElementById){
if (event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if(document.layers){
if(event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
}
// -->
</SCRIPT>";
Page.RegisterStartupScript("ForceDefaultToScript", theScript);
textControl.Attributes.Add("onkeydown", "fnTrapKD(" + defaultButton.ClientID + ",event)");
}
Then in my Page_Load method I called:
Code: C#
SetDefaultButton(this.Page, PDFUrl, PDFUploadButton);
When the user types return in the text box, however, an Exception is thrown by function _eofi_a().
The message is "b has no properties".
Is there a way you would recommend to direct the return key to call click()?
- Paul