Default Button on web form

G

Geoff

I have translated the DefaultButton() function, available at

http://www.kamp-hansen.dk/pages/showdoc.asp?id=28&menuid=21&menuid=18

into C# but I am experiencing a problematic side-effect. If you
double click a text box, and IE pops up a list of the values you have
recently used, and you arrow key down, or move the mouse down, to
highlight one, and THEN hit the enter key, the Enter get processed,
and the button gets clicked, BEFORE the text makes it into the
TextBox. Can anyone provide a solution to this problem?

using System.Text;

public void DefaultButton(System.Web.UI.Page Page, TextBox
objTextControl, Button objDefaultbutton)
{
StringBuilder sScript = new StringBuilder();

sScript.Append("<SCRIPT language=\"javascript\">" +
Environment.NewLine);
sScript.Append("function fnTrapKD(btn){" + Environment.NewLine);
sScript.Append(" if (document.all){" + Environment.NewLine);
sScript.Append(" if (event.keyCode == 13)" + Environment.NewLine);
sScript.Append(" { " + Environment.NewLine);
sScript.Append(" event.returnValue=false;" +
Environment.NewLine);
sScript.Append(" event.cancel = true;" + Environment.NewLine);
sScript.Append(" btn.click();" + Environment.NewLine);
sScript.Append(" } " + Environment.NewLine);
sScript.Append(" } " + Environment.NewLine);
sScript.Append("}" + Environment.NewLine);
sScript.Append("</SCRIPT>" + Environment.NewLine);

objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." +
objDefaultbutton.ClientID + ")");
Page.RegisterStartupScript("ForceDefaultToScript",
sScript.ToString());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top