Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 !

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

Posted

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

Posted

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 !

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

Posted

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

  • 1 year later...
Posted
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)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...