asp: LinkButton, how to cancel the OnClick event?

advertisements

I have searched for hours, but I am lost.

Is there any way to Cancel a OnClick() event from an asp:LinkButton command?

I have the following client side code:

<asp:LinkButton ID="LinkButton3" runat="server"  CausesValidation="False" CommandName="Delete" OnInit="SetVisibility"  OnClientClick="return confirm('Are you sure?');"  Text="Delete" OnClick="LinkButton3_Click"></asp:LinkButton>

The server side code for the OnClick() event is thus ...

//Trap the delete button
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        try
        {
            if (ListBox1.SelectedItem.Text == "** NO SCHOOL RECORDED **")
                throw new Exception("You cannot delete this entry, as it's the default for every student until a School is selected in Basic Enrolments.");
        }
        catch (Exception exc)
        {
            webMessageBox.Show(exc.Message);
            return;
        }
    }

As you can see I want to abort the Delete command if the dropdown in my code has a specific Text.

The Return; statement does nothing. The record still gets deleted!

Is there a way to abort this event, since there is no e.Cancel method?

I've read [this][1] and [this][2], to no avail. The suggestion that there is no way to cancel this event, makes me think that perhaps I should be aborting the delete in the databinding events? Or even better, how to hide the Delete linkbutton if the user selects the above dropdown text?

Thanks in advance.

Thanks


Instead doing it on server side you can do it on client side. You can use require filed validator and use that.

Examples

  1. How to place a required field validator inside a GridView TextBox
  2. How to add a RequiredFieldValidator to DropDownList control?

Or create a javascript or jQuery function on client click and check the dropdown value and do the validation.