Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

Sponsored Links

Discuss "Webform Default Button" in the .Net Development forum.

.Net Development - Learn about the Microsoft.Net framework and how to create powerful web-based (ASP.net) and client-based (Windows Forms) applications utilizing various languages such as C#, VB.Net, J# and others.


Closed Thread « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old March 31st, 2008, 10:03 PM
Barn Newbie
 
Join Date: Mar 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Devwhiz is an unknown quantity at this point
Default Webform Default Button

Hi, I have a situation which is causing me to pull the few last hairs on my head.

I have this Asp.Net form in which I have defined a default button: btnFind
and I have it defined as follows:
Code:
<form id=”form1″ runat=”server” defaultbutton=”btnFind″ >
This works great in both IE and Firefox but when I add an UpdatePanel
something really strange happens, it doesn't work on Firefox.

I'm really stumbled to make this work.

I'm using Visual Studio 2005 .Net Framework 2.0, and Ajax

Can someone please help me?
Sponsored Links
  #2  
Old April 1st, 2008, 05:48 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

A solution I've found, provided you either have 1 button on the page, or the default button is the first that's rendered is to use Javascript.

Within the head of your content page include:-
Code:
<script type="text/javascript">
function press(evt)
{
if(evt.which || evt.keyCode){
     if ((evt.which == 13) || (evt.keyCode == 13)) {
          //Use the id of the button you want as the default.
          var buttonid = '<%=Button1.ClientID %>';
          __doPostBack(buttonid,'');
          returntrue;
      }
 } else {
      returnfalse;
     }
}
</script>
Add it as an attribute to the form in code behind, using onkeypress:-
Code:
sub Page_Load()
 
  if not ispostback then
        form1.attributes.add("onkeypress", "press(event);")
  end if
 
end sub
Hope that helps in some way. Perhaps someone else can provide an example that works in all instances.

Last edited by richyrich; April 1st, 2008 at 08:45 AM. Reason: Tidied up code
  #3  
Old April 1st, 2008, 12:26 PM
Barn Newbie
 
Join Date: Mar 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Devwhiz is an unknown quantity at this point
Default

Thanks for your reply richy
I get the following error with the code you provided:

System.Web.UI.HtmlControls.HtmlForm' does not contain a definition for 'attributes'
  #4  
Old April 1st, 2008, 02:24 PM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Strange....It worked for me.

Could you post the code you have so far?
  #5  
Old April 1st, 2008, 02:42 PM
Barn Newbie
 
Join Date: Mar 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Devwhiz is an unknown quantity at this point
Default

Thanks for your help, I still can't figure it out
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Employee Listing</title>
    <script type="text/javascript">
    function press(evt)
    {
      if(evt.which || evt.keyCode){
        if ((evt.which == 13) || (evt.keyCode == 13)) {
          //Use the id of the button you want as the default.
          var buttonid = '<%=Button1.ClientID %>';
          __doPostBack(buttonid,'');
          returntrue;
        }
       } else { returnfalse; }
    }
</script>   
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="MyScriptManager" runat="server" />    
    <asp:updatepanel ID="MyUpdatePanel" runat="server">
    <ContentTemplate>
	<asp:Label ID="msg" runat="server" />
	<asp:TextBox ID="txtName" runat="server" />
	<asp:Button ID="btnFind" runat="server" Text="Find" onclick="btnFind_Click" />
    </contentTemplate>
    </form>
</body>
</html>
my code behind is
Code:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
      form1.attributes.add("onkeypress", "press(event);")
}

protected void btnFind_Click(object sender, EventArgs e)
{
    msg.Text = "The Name to Search for is " + txtName.Text;
}

Last edited by Devwhiz; April 2nd, 2008 at 10:39 AM.
  #6  
Old April 1st, 2008, 06:13 PM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Don't know if this will have an effect, but you hadn't changed the id of the button to be default in the JS and there were a couple of spaces missing. You were also missing the end UpdatePanel tag. Also, add the onkeypress to the form tag itself and remove the form1.attributes.add line completely from your code behind.
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Employee Listing</title>
    <script type="text/javascript">
    function press(evt)
    {
         if(evt.which || evt.keyCode){
              if ((evt.which == 13) || (evt.keyCode == 13))
              {
                  //Use the id of the button you want as the default.
                  var buttonid = '<%=btnFind.ClientID %>';
                  __doPostBack(buttonid,'');
                  return true;
               }
          } else {
               return false;
          }
       }
</script>   
</head>
<body>
    <form id="form1" runat="server" onkeypress="press(event);">
    <asp:ScriptManager ID="MyScriptManager" runat="server" />    
    <asp:updatepanel ID="MyUpdatePanel" runat="server">
    <ContentTemplate>
    <asp:Label ID="msg" runat="server />
    <asp:TextBox ID="txtName" runat="server" />
    <asp:Button ID="btnFind" runat="server onclick="btnFind_Click" />
    </contentTemplate>
    </asp:updatepanel>
    </form>
</body>
</html>

Last edited by richyrich; April 1st, 2008 at 06:16 PM.
  #7  
Old April 2nd, 2008, 11:28 AM
Barn Newbie
 
Join Date: Mar 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Devwhiz is an unknown quantity at this point
Default

Thanks richy, this works

But what if I had different tabpanels?

Would this work?
  #8  
Old April 2nd, 2008, 11:30 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Quote:
Originally Posted by Devwhiz View Post
Thanks richy, this works

But what if I had different tabpanels?

Would this work?
You mean more than 1 updatepanel on a page?
  #9  
Old April 2nd, 2008, 04:18 PM
Barn Newbie
 
Join Date: Mar 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Devwhiz is an unknown quantity at this point
Default

yes
  #10  
Old May 15th, 2008, 01:15 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

As this thread has been inactive for more than 30 day -- it has been closed.
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -4. The time now is 08:02 PM.



Content Relevant URLs by vBSEO 3.2.0