Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hey everyone,
 
I have a button in which I am using both OnClientClick and OnClick.
 
The onClientClick works well, but when onClientClick condition is not met, Server side click event is not fired.
 
 <asp:Button ID="btnDelete" runat="server" Text="Delete" Style="float: left; margin-right: 10px; color: black; font-weight: bold; margin-left: 40px" Height="26px" Width="67px" OnClientClick = "javascript:selectedItems(); return false;" OnClick = "btnDelete_Click" CausesValidation="false" />
 
function selectedItems()
         {
          var MasterTable = $find("<%=RadGrid1.ClientID%>").get_masterTableView();
          var selectedItemsCount = MasterTable.get_selectedItems().length;
          if (selectedItemsCount == 0) {
              alert("Select at least one item!");
              return false;
                       }
                     }
Posted 1-Nov-10 20:48pm
Edited 1-Nov-10 22:28pm
Dalek Dave432.2K
v2
Comments
Hiren Solanki at 2-Nov-10 3:08am
   
try by return true inside client event at end.
Dalek Dave at 2-Nov-10 4:27am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are returning 'false' always . ie is the issue.
Change the calling of javascript function as OnClientClick="javascript: return selectedItems();"

 

Hope this will help you Smile | :)
  Permalink  
Comments
Dalek Dave at 2-Nov-10 4:27am
   
Good Call.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
your problme is in this line
 
 
OnClientClick="javascript:selectedItems();return false;"
 
if "return false" is execute then it would never submit and OnClick(which is sever side would never executed).
 
"return false" statement should be conditional.
 
change the code like this:
 
OnClientClick="javascript:selectedItems();"
and in the defination of selectedItems() javascript function
 
if (selectedItemsCount == 0) 
{                       
    alert("Select at least one item!");
    return false;
}
else
{
    // this path ensures that when user selects at least 1 item then it 
    // proceeds to server side code OnClick to be executed.
    return true;
}
 
Hope that helps,
 
Thanks
Arindam D Tewary,
  Permalink  
Comments
Dalek Dave at 2-Nov-10 4:27am
   
Good Answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
I have a ASP button in my web application as:-
 
<asp DropDownList ID="dropDown1" runat="server">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem value="reject">Reject</asp:ListItem>
    <asp:ListItem value="accept">Accept</asp:ListItem>
</asp DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></<asp:TextBox>
 
<asp:Buton ID="button7" runat="server" CommandName="update" CssClass="button"
onclientclick="return checkDecision();" onclick="button7_Click" Text="Submit"
UseSubmitBehavior="False">
 
<script type="text/javascript">
 
function checkDecision(){
     if(document.getElementByID("dropDown1").value == "reject" && document.getElementById("TextBox1").value == ""){
     alert("Please fill the textbox");
     return false;
     }
     else{
        return true;
     }
</script>
 
On clicking the submit button the checkDecision() function is getting called properly. When it returns true that is dropDown is selected with reject and textbox is not empty then "button7_click" function on server side is not getting called..
 
Can any1 please help me with this ?? Thank You in advance....
 
Regards,
~Sorabh
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 2 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100