TL;DR: If the Edit button is clicked without a ListBox selection, triggering a validation failure, why does the Delete
link need to be clicked twice before the browser responds?
Using the source code below for a very simple menu, I have three links:
New
- does not perform validation and redirects to the "create" formEdit
- checks that a ListItem is selected, and if so, redirects to the "edit" form with an ID value supplied as a parameterDelete
- redirects to the "delete" page, regardless of whether a selection is made or not
I have noticed a strange behaviour, and have reproduce in this simple one-page solution (download link from OneDrive):
- Load page
- Click on
Edit
without making a selection from the ListBox (validation error appears) - Click on
Delete
- nothing happens - why? - Click on
Delete
- server-side redirect occurs successfully, but this should happen on step 3 above
Here is what happens in my browser (tested in Chrome, FF and Edge) - notice that two clicks are required on the Delete link:
Update
The problem appears to be within ASP.Net's form submit code:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
// On first click - this logic path is never reach, but on the second click, it is reached
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
I am trying to figure out why the if
statement returns false, after I have manually called Page_ClientValidate()
.
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Page</title>
<script>
function RedirectToPageWithSelectedId() {
if (Page_ClientValidate("mustSelect")) { // Asp.Net client-side validation, passing ValidationGroup as parameter
var li = $("#<%=ListBox1.ClientID%> option:selected");
var i = li.val();
if (parseInt(i) != NaN) {
window.location.href = "/edit/" + i;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:ListBox ID="ListBox1" runat="server" />
<asp:RequiredFieldValidator ID="Rfv1" runat="server" Text="Selection required" ValidationGroup="mustSelect" ControlToValidate="ListBox1" Display="Dynamic" />
</p>
<ul>
<li>
<asp:HyperLink ID="lnk_new" runat="server" Text="New" NavigateUrl="~/new" />
</li>
<li>
<asp:HyperLink ID="lnk_edit" runat="server" Text="Edit Selected" onclick="RedirectToPageWithSelectedId(); return false;" href="#" />
</li>
<li>
<asp:LinkButton ID="lnk_delete" runat="server" Text="Delete" />
</li>
</ul>
</div>
</form>
</body>
</html>
I can update to provide the page's code-behind if required; just ask.