Default buttons

ultraman

Centurion
Joined
Dec 31, 1969
Messages
138
Location
Quebec, Canada
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 !
 
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
 
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
 
bungpeng said:
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)
 
Back
Top