ultraman Posted May 7, 2003 Posted May 7, 2003 Is there any way to set a default button on an aspx page ? I have just 2 buttons, but it seems that the wrong one is the default. I haven't done anything but to put these buttons on the form.... Thanks again ! Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
bungpeng Posted May 8, 2003 Posted May 8, 2003 In web client site (HTML environment), default button means "submit" button, because normally there is only 1 submit button in HTML. But in ASP.NET, every button control (server) is a "submit" button in client site, so I don't think you can define the default button. My suggestion is, if you really want that, maybe one of the stupid way is: you can use scripting to help you (focus on that button). Please let me know if you find any better way or ASP.NET did provided this feature Quote
ultraman Posted May 8, 2003 Author Posted May 8, 2003 Haven't found anything yet, but I keep on searching. If I find something (other than scripting) I'll post it in this thread. Thanx ! Quote Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone! --Homer Simpson
Mayfield2268 Posted May 8, 2003 Posted May 8, 2003 Here is a procedure that might help depending on what your actually doing. This procedure allows you to set a default button for different controls such as a textbox. Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button) ' Sets default buttons. ' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk Dim sScript As New System.Text.StringBuilder() sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf) sScript.Append("function fnTrapKD(btn){" & vbCrLf) sScript.Append(" if (document.all){" & vbCrLf) sScript.Append(" if (event.keyCode == 13)" & vbCrLf) sScript.Append(" { " & vbCrLf) sScript.Append(" event.returnValue=false;" & vbCrLf) sScript.Append(" event.cancel = true;" & vbCrLf) sScript.Append(" btn.click();" & vbCrLf) sScript.Append(" } " & vbCrLf) sScript.Append(" } " & vbCrLf) sScript.Append("}" & vbCrLf) sScript.Append("</SCRIPT>" & vbCrLf) objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")") Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString) End Sub Hope this helps Quote
mhsueh001 Posted May 27, 2004 Posted May 27, 2004 In web client site (HTML environment), default button means "submit" button, because normally there is only 1 submit button in HTML. But in ASP.NET, every button control (server) is a "submit" button in client site, so I don't think you can define the default button. My suggestion is, if you really want that, maybe one of the stupid way is: you can use scripting to help you (focus on that button). Please let me know if you find any better way or ASP.NET did provided this feature I found this on the web: Add the following line to your page's Load event, replacing "btnSearch" with the name of your button. It uses a hidden Page method called RegisterHiddenField. Page.RegisterHiddenField("__EVENTTARGET", "btnSearch") Note: Intellisense doesn't show this event/method so when you type Page.RegisterHiddenField (RegisterHiddenField doesn't show up in the list. However after you hit the parenthesis it does shows the parameters necessary) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.