Wednesday, August 10, 2005 - Posts

Default button for a textbox in ASP.NET

This trips people up again & again and I tend to have to look it up too, so I'm linking to the solution by Raj Kaimal.

Hitting the enter key in a TextBox can sometimes has undesired effects like the wrong submit Button being “clicked“. The method described below allows you to specify a default Button to submit when the user hits the enter key in a TextBox.

//client side js
function clickButton(e, buttonid) {
      var bt = document.getElementById(buttonid);
      if (typeof bt == 'object') {
            if(navigator.appName.indexOf("Netscape")>(-1)) {
                  if (e.keyCode == 13) {
                        bt.click();
                        return false;
                  }
            }
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)) {
                  if (event.keyCode == 13) {
                        bt.click();
                        return false;
                  }
            }
      }
}

//code behind
TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')"  />


Also on the web front, Mikhall Arkhipov notes how you can associate "files-behind" in Visual Web Developer. So where you have .gif files that you have sliced from a mockup in Macromedia Fireworks you can associate the original .png file in the IDE. Guessing this is new to Visual Studio 2005?

with 0 Comments