开发者

imagebutton with onclientclick isn't firing onclick event

开发者 https://www.devze.com 2023-02-03 16:17 出处:网络
I have an imagebutton with an postbackurl and an onclientclick script.When i added the onclientclick code, if my javascript validation passes (aka returns true), the page just seems to perform a postb

I have an imagebutton with an postbackurl and an onclientclick script. When i added the onclientclick code, if my javascript validation passes (aka returns true), the page just seems to perform a postback (the screen just seems to refresh itself), rather than post to the postbackurl. Any ideas why this is happening?

Sample:

<asp:ImageButton ID="imgSendInfo" runat="server" SkinID="SendInfo" PostBackUrl="MyUrlOnAnotherSite" onClientClick="javasc开发者_运维技巧ript:return onFormSubmit(this.form);return document.MM_returnValue" />

UPDATE:

OK, so I decided to change what JS functions I'm calling now since calling Multiple functions definitely wasn't helping. Here's my updated code. All I'm doing now is validating a single textbox and returning true or false. Even this simple function is causing the postback URL to never get called. Could it have anything to do with the fact that I'm trying to call a function to return a true or false?

My validation function:

function valForm() {
    if (document.getElementById('FName').value == '') {
        alert('no');
        return false;
    }
    else {
        alert('yes');
        return true;
    }
}

My ImageButton:

<asp:ImageButton ID="imgSendInfo" runat="server" SkinID="SendInfo" PostBackUrl="SetOnCodeBehind" onClientClick="javascript:return valForm();" />


OK figured out a workaround. I REMOVED the return statement from the onclientclick, since the return is what was messing with the postback. I then added requiredfieldvalidators to the page, but Im not displaying any text. This way, 2 sets of validation are occurring (booo), but the first displays my alert messages (this is how the client wants validation performed), and the second prevents the form from posting.

My imagebutton:

<asp:ImageButton ID="imgSendInfo" runat="server" SkinID="SendInfo" PostBackUrl="SetOnCodeBehind" ValidationGroup="enroll" CausesValidation="true" onClientClick="javascript:onFormSubmit(this.form);document.MM_returnValue;" />

My requiredfieldvalidation group:

<asp:RequiredFieldValidator ID="reqVal1" runat="server" ErrorMessage="" ValidationGroup="enroll" ControlToValidate="FName" InitialValue="" />
<asp:RequiredFieldValidator ID="reqVal2" runat="server" ErrorMessage="" ValidationGroup="enroll" ControlToValidate="LName" InitialValue="" />


Did you know that your onClientClick js-function returns twice? return document.MM_returnValue never gets reached.

Is your PostBackUrl's page in your application? You can even validate the previous page on serverside:

If Page.PreviousPage.IsValid Then
    ' Handle the post back
Else
    Response.Write("Invalid")
End If

For further information: MSDN LinkButton.PostBackUrl

0

精彩评论

暂无评论...
验证码 换一张
取 消