Results 1 to 6 of 6

Thread: How to suppress server control event (onclick)

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    How to suppress server control event (onclick)

    Hello folks,

    I have a simple form with a button. The button is server control.

    in the OnClientclick event of the button control, I am actually doing some validations. If the validation fails, the onclick event should not be executed. How can I control this?

    In the code below, PerformChecks() function gets fired first and alert message pops up fine. But immediately onclick event (which is handled on the server) gets triggered. if the check in PerformChecks fail, how can I suppress onclick event?

    Code:
     <asp:Button ID="ButtonRun" runat="server" Text="Go" 
                        onclick="ButtonRun_Click" OnClientClick="PerformChecks();" Width="57px"  />
    
    
    function PerformChecks() {
                     
                     
      var checkboxCollection = document.getElementById('<%=CheckBoxListInstruments.ClientID %>').getElementsByTagName('input');
    
      for (var i = 0; i < checkboxCollection.length; i++) {
    
              if (checkboxCollection[i].checked)
                   return;
      }
    
       window.alert("Please check atleast one checkbox");
                        
       }                  
       }

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to suppress server control event (onclick)

    Put a

    return false;

    after the window.alert

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Re: How to suppress server control event (onclick)

    szlamany,

    Thanks much. It worked like a charm.

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to suppress server control event (onclick)

    Are you actually doing work in the Server Side OnClick event? What I am getting at is, do you need the Button to be an ASP.Net Server control?

    On another note, in order to tie with the other validation functionality that exists in ASP.Net, you might want to think about creating your own Custom Validator:

    http://www.4guysfromrolla.com/articles/073102-1.aspx

    Here is a complete sample that shows how you can do just that for the case of a CheckBoxList, which is what you are using:

    http://www.4guysfromrolla.com/webtec...040302-1.shtml

    Hope that helps!

    Gary

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Re: How to suppress server control event (onclick)

    Gary,

    Yes I have to use server control, because I doing lot of stuff on the server.

    Thanks for your response

  6. #6
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to suppress server control event (onclick)

    In which case, that makes sense then

    Can you remember to mark your thread as resolved?

    Thanks

    Gary

Posting Permissions

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



Click Here to Expand Forum to Full Width