0

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

Fandango68
  • 4,461
  • 4
  • 39
  • 74
  • 1
    Why not just use the command event instead and if the e.CommandName is delete, do a check to see if any conditions are met where you don't want to proceed and just wrap that conditionally. – Mark Fitzpatrick Feb 04 '15 at 03:56
  • But how do I abort the OnCommand() event? Return;? – Fandango68 Feb 04 '15 at 04:05
  • Don't abort it. Just short circuit it before calling your function that will delete a record with conditional logic. – Mark Fitzpatrick Feb 04 '15 at 04:08
  • Sorry mate, but what do you mean by "short circuit"? Not familiar with all of ASP.net speak yet. – Fandango68 Feb 04 '15 at 04:40
  • If you solved the problem, why not answer your own question and mark it as Answer? – Win Feb 04 '15 at 05:05
  • Why can't you check ListBox1.SelectedItem.Text == "** NO SCHOOL RECORDED **" at client side itself in OnClientClick? – Amit Feb 04 '15 at 05:22
  • "short circuit" is a carryover from the electronics world. Essentially if you have a process that is going, such as a delete, you can add conditions to essentially terminate the process early. Generally it means putting logic in place to stop a process when certain conditions are met. – Mark Fitzpatrick Feb 05 '15 at 00:35
  • @MarkFitzpatrick. LOL. Sorry I know what short-circuit means. What I mean is "what do you mean by short-circuiting" the OnClick() event? Like I said, I tried "return" and it still executes the event to the end, which ends up deleting the record - which I DON'T want. I wanted it to abort the event entirely like using an e.Cancel. – Fandango68 Feb 05 '15 at 04:50
  • @Win because it's not an answer. It's a work-around. – Fandango68 Feb 05 '15 at 04:50
  • @Amit. That's not a solution. I am already doing that. What I want is a way to abort the OnClick() event when a condition is met. Read my response to Mark's comments. Thanks – Fandango68 Feb 05 '15 at 04:51
  • Why you want to abort instead of gracefully handling and exiting? You are already in event and you want to cancel?? – Amit Feb 05 '15 at 05:00
  • @Amit. I'll re-phrase. I am simply trying to avoid the OnClick() event from DELETing my record when a condition is met. That's as simple as I can put it. That's why I asked how to abort the event like the e.Cancel=true does. If there is another way (or this "short circuit"), please tell me. Thanks – Fandango68 Feb 05 '15 at 05:05
  • Please put some code to show how delete code get fired. – Amit Feb 05 '15 at 05:36
  • @Fernando68. In your case, it looks like the OnClick was the wrong one to choose. It doesn't look like you have logic in your onclick that is handling any delete. Most likely because you have CommandName="Delete" defined. Normally, all you would do is: if(!someconditionYouDontWantDeleteToOccur) { // then delete. } . I think in your case it's misleading. You have a command that says CommandName="Delete". That means that the OnClick most likely isn't doing the delete operation. The containing control, your DetailsView, is probably handling that in the ItemCommand event, not the OnClick – Mark Fitzpatrick Feb 05 '15 at 05:37

2 Answers2

0

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.

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • Thanks @Shekha, but I am after a C# solution on how to abort the OnClick() event. Nothing to do with jquery, scripting, etc. – Fandango68 Feb 05 '15 at 04:53
0

UPDATE

I've decided to ditch the OnClick() event entirely and trap the condition using the DetailView's DataBound event. I then checked the value from the dropdownlist and simply hide/show the LinkButton controls as necessary.

ie:

 protected void DetailsView1_DataBound(object sender, EventArgs e)
 {
     LinkButton lnk1 = (LinkButton)DetailsView1.FindControl("LinkButton1");
     LinkButton lnk2 = (LinkButton)DetailsView1.FindControl("LinkButton2");
     LinkButton lnk3 = (LinkButton)DetailsView1.FindControl("LinkButton3");
     if (ListBox1.SelectedItem.Text == "** NO SCHOOL RECORDED **")
     {
         if (lnk1 != null) lnk1.Visible = false;
         if (lnk2 != null) lnk2.Visible = false;
         if (lnk3 != null) lnk3.Visible = false;
     }
     else
     {
         if (lnk1 != null) lnk1.Visible = true;
         if (lnk2 != null) lnk2.Visible = true;
         if (lnk3 != null) lnk3.Visible = true;
     }
 }
Fandango68
  • 4,461
  • 4
  • 39
  • 74