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
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;
}
}