Click here to Skip to main content
15,906,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
<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" />


C#
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
Updated 1-Nov-10 22:28pm
v2
Comments
Hiren solanki 2-Nov-10 3:08am    
try by return true inside client event at end.
Dalek Dave 2-Nov-10 4:27am    
Edited for Grammar and Readability.

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



Hope this will help you :)
 
Share this answer
 
Comments
Dalek Dave 2-Nov-10 4:27am    
Good Call.
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,
 
Share this answer
 
Comments
Dalek Dave 2-Nov-10 4:27am    
Good Answer.
XML
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900