Closed Thread
Results 1 to 10 of 10

Thread: Webform Default Button

  1. #1
    Barn Newbie Devwhiz is an unknown quantity at this point Devwhiz's Avatar
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    4

    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?

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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 09:45 AM. Reason: Tidied up code

  3. #3
    Barn Newbie Devwhiz is an unknown quantity at this point Devwhiz's Avatar
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    4

    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. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Strange....It worked for me.

    Could you post the code you have so far?

  5. #5
    Barn Newbie Devwhiz is an unknown quantity at this point Devwhiz's Avatar
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    4

    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 11:39 AM.

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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 07:16 PM.

  7. #7
    Barn Newbie Devwhiz is an unknown quantity at this point Devwhiz's Avatar
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    4

    Thanks richy, this works

    But what if I had different tabpanels?

    Would this work?

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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. #9
    Barn Newbie Devwhiz is an unknown quantity at this point Devwhiz's Avatar
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    4

    yes

  10. #10
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

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

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO