Tuesday, July 28, 2009

Setting Default button in ASP

The other day I saw a posting on a forum about setting Default button for a web form. Till that day I had not tried that thing out (I just vaguely remembered that there's something like a DefaultButton property). So when I created a simple form with a text box, button and a gridview wrapped in an Update Panel, I just couldn't locate the property anywhere (no property in asp:Button tag, no this.DefaultButton). Then I read that to achieve the default button property you need to do:
ClientScript.RegisterHiddenField("__EVENTTARGET", submitButton.UniqueID);
Once the code was in place and I hit the "enter" key and lo and behold another cryptic error
Microsoft JScript runtime error: 'this._postBackSettings.async' is null or not an object
Then I read on another blog that you need to put a panel around and set the DefaultButton property value. Though this was not the response I was looking for but I got an idea as to where to look for the DefaultButton thing. So, I removed my ClientScript.RegisterHiddenField code and set the
defaultbutton="submitButton"
in form tag. And that was it!

No comments: