views:

927

answers:

2

This query is related to this one I asked yesterday. I have a radio button list on my asp.net page defined as follows:

<asp:RadioButtonList ID="rdlSortBy" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" AutoPostBack="True" >
       <asp:ListItem Selected="True">Name</asp:ListItem>
        <asp:ListItem>Staff No</asp:ListItem>
</asp:RadioButtonList>

On the client-side, am doing the following validations:

rdlSortBy.Attributes("onclick") = "javascript:return prepareSave() && prepareSearch();"

The problem is that the Javascript validation runs as expected. A message is displayed and I expect to remain on the page until user has saved the changes, instead the page is posted back and effectively I am loosing unsaved changes.

What could be going wrong?

+1  A: 

The OnClick code you are using to "validate" is being run and then the code which posts the form back which the control itself injects is being run.

You need to intercept that PostBack process and stop it before it posts the form client-side. The best way to do this would be with a CustomValidator and specifically the "ClientValidationFunction" property. This will allow your JavaScript to signal that the post back should not occur due to a validation error.

Aydsman
+2  A: 

I fixed this, the problem was that I was attaching the "onclick" of the RadioButtonList instead on the individual radio buttons.

This is the fix:

rdlSortBy.Items(0).Attributes("onclick") = "javascript:return isDirtied() && prepareSearch();"
    rdlSortBy.Items(1).Attributes("onclick") = "javascript:return isDirtied() && prepareSearch();"
J Angwenyi