This is a migrated thread and some comments may be shown as answers.

OnClientClick breaks button postback behavior

2 Answers 302 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pmourfield
Top achievements
Rank 1
pmourfield asked on 12 Jul 2013, 02:36 PM
I have a RadGrid Insert/Edit Form template in which I have a Submit/Update button. This button's submit behavior works perfectly if I do not reference any JavaScript calls in it. However, upon inserting an OnClientClick, it breaks the button. Here is the ASP

<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Save" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' OnClientClick="return CheckWeight()" />

Here is the JavaScript

function CheckWeight() {
                var currentGoalWeightTotal = $("[id$='CurrentGoalWeightTotal']").val();
                var weightInput = $("[name$='AG_Weight']").val();
 
                if (parseInt(weightInput) + parseInt(currentGoalWeightTotal) > 100) {
                    alert("Please make sure that your goal weight total does not exceed 100.");
                    return false;
                }                   
            }

2 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 17 Jul 2013, 10:36 AM
Hello Joshua,

I have prepared a sample RadGrid web site to demonstrate how you can achieve the requested functionality. Please run the attached application and let me know if it helps you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
pmourfield
Top achievements
Rank 1
answered on 17 Jul 2013, 12:11 PM
Eyup,

Thank you for putting this together for me. I will definitely check it out.

I was able to get my code working by making the following changes:

I changed my ASP to this:

<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Save" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' OnClientClick="if (!CheckWeight()) { return false;}" UseSubmitBehavior="false" />

And changed my JavaScript to this:

function CheckWeight() {
                var currentGoalWeightTotal = $("[id$='CurrentGoalWeightTotal']").val();
                var weightInput = $("[name$='AG_Weight']").val();
 
                if (parseInt(weightInput) + parseInt(currentGoalWeightTotal) > 100) {
                    alert("Please make sure that your goal weight total does not exceed 100.");
                    return false;
                } else {
                    return true;
                }
            }
Tags
Grid
Asked by
pmourfield
Top achievements
Rank 1
Answers by
Eyup
Telerik team
pmourfield
Top achievements
Rank 1
Share this question
or