locked
Please explain RRS feed

  • Question

  • Hi

    I have a peculiar problem here and I can't by my life figure out what the solution is. Note that the following code is not dynamically created, but just immediately in my aspx file.

    <button type="button" runat="server" id="btnSubmit"
      OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?');">
        Submit
    </button>
    This works just fine as long as I don't have the onclick attribute there, i.e. the OnServerClick handler is fired as it should. But when I use the onclick attribute it is not, no matter whether I confirm or decline the confirmation dialog box.

    Even if I do like following for is never submitted
    <button type="button" runat="server" id="btnSubmit"
      OnServerClick="btnSubmit_Click" onclick="return true;">
        Submit
    </button>

    What am I doing wrong? Thanks

    • Moved by David M Morton Tuesday, September 1, 2009 8:35 PM ASP.NET Question (From:.NET Base Class Library)
    Tuesday, September 1, 2009 6:01 AM

Answers

  • Your HTML code isn't correct.  Here's how you create an ASP.NET button that has both a client and server side click handler.

    <

     

     

    asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirm('Are you sure?');" OnClick="Button1_Click" />

    Michael Taylor - 9/1/09
    http://p3net.mvps.org
    • Proposed as answer by mott555 Tuesday, September 1, 2009 8:26 PM
    • Marked as answer by David M Morton Tuesday, September 1, 2009 8:35 PM
    Tuesday, September 1, 2009 1:55 PM
  • I know this.What I am trying to ask is if u use a html button with a runat=server and put a onclick event handler then ur form will never be submitted and the reason for this is that when u put a onclick event handler for a button internally it is conveter as
    onclick="whatervedr user code is there;return true;do_postback();"
    Since return true comes before do_postback it is never submitted to the server.

    I would suggest u to put a html button with runat=server and put a onclick event and try to  submit ur page and see the problem
    • Marked as answer by Me_Learner Friday, September 4, 2009 11:40 AM
    Friday, September 4, 2009 11:40 AM

All replies

  • Your HTML code isn't correct.  Here's how you create an ASP.NET button that has both a client and server side click handler.

    <

     

     

    asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirm('Are you sure?');" OnClick="Button1_Click" />

    Michael Taylor - 9/1/09
    http://p3net.mvps.org
    • Proposed as answer by mott555 Tuesday, September 1, 2009 8:26 PM
    • Marked as answer by David M Morton Tuesday, September 1, 2009 8:35 PM
    Tuesday, September 1, 2009 1:55 PM
  • TaylorMichaelL is correct. ASP.NET buttons have two important attributes. OnClick is server-side and must point to a valid CLR event handler within the page or its code-behind file. OnClientClick is client-side and you can put any JavaScript code in there that you want, including function calls to JavaScript library functions (such as 'confirm()') or to any custom JavaScript functions.

    I'm not aware of OnServerClick being a valid attribute for asp:Button.
    Tuesday, September 1, 2009 8:30 PM
  • I know this.What I am trying to ask is if u use a html button with a runat=server and put a onclick event handler then ur form will never be submitted and the reason for this is that when u put a onclick event handler for a button internally it is conveter as
    onclick="whatervedr user code is there;return true;do_postback();"
    Since return true comes before do_postback it is never submitted to the server.

    I would suggest u to put a html button with runat=server and put a onclick event and try to  submit ur page and see the problem
    • Marked as answer by Me_Learner Friday, September 4, 2009 11:40 AM
    Friday, September 4, 2009 11:40 AM