J'ai un projet webforms auparavant opérationnel que j'ai dû inclure dans une solution MVC préexistante et dans lequel il y a un contrôle LinkButton dans un formulaire qui ne fonctionne que partiellement; son gestionnaire d'événements JavaScript fonctionne parfaitement bien (référencé ci-dessous par OnClientClick) mais le code-behind C# (lnkSubmit_Click) n'est pas invoqué! Pourquoi pas?Pourquoi le formulaire Web ASP.NET LinkButton de mon projet MVC n'invoque-t-il pas son gestionnaire d'événement OnClick derrière code?
Mes LinkButton est à l'intérieur Pay.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pay.aspx.cs" Inherits="PayUp.Pay" MasterPageFile="~/Views/Shared/Site.Master" %>
<form id="form1" runat="server" onsubmit="return validateForm();" defaultbutton="lnkSubmit" action="~/Views/Home/Pay.aspx" method="post" >
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div align="center">
<script type="text/javascript">
window.onload = function() {
hideFields();
};
</script>
<asp:UpdatePanel runat="server" id="PendingPayments1">
<ContentTemplate>
<!-- Valid form controls, blah blah.... -->
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="labelPayPlan" runat="server" Text="Pay Plan">
</asp:Label>
<asp:HiddenField ID="hiddenPDFForm" Value="false" runat="server" ClientIDMode="Static" />
<asp:UpdatePanel runat="server" id="PaymentInformation1">
<ContentTemplate>
<asp:LinkButton ID="lnkSubmit" runat="server" EnableViewState="false" OnClientClick="javascript: document.getElementById('hiddenPDFForm').value = 'false';"
CssClass="button" onclick="lnkSubmit_Click" onserverclick="lnkSubmit_Click" ClientIDMode="Static">Pay Up!<img alt='Pay Up!' src='images/pay.gif' style='border:0px; margin-left:6px;' />
</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</form >
Le code-behind n'existe à l'intérieur Pay.aspx.cs:
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Configuration;
using SupportingMVCProject;
using System.Web.Mvc;
namespace PayUp
{
public partial class Pay : System.Web.Mvc.ViewPage<SupportingMVCProject.Account>
{
protected void Page_Load(object sender, EventArgs e)
{
// The code in this event handler runs just fine when I start the project!
// This code successfully receives and correctly parses my URL parameters (QN, PN, AC).
}
protected void lnkSubmit_Click(object sender, EventArgs e)
{
// The code in this event handler never gets to run!
}
}
}
Mon RouteConfig.cs ressemble:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
namespace MainMVCProject
{
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{WebForm}.aspx/{*pathInfo}");
routes.MapRoute("PayUpIndex", "SupportingMVCProject/PayUp",
new { controller = "PayUp", action = "Index" }
);
// Other valid preexisting MVC routes...
// blah, blah
}
}
}
La route PayUpIndex de PayUpController.cs identifiée ci-dessus ressemble à ceci et fonctionne:L'URL de départ est http://localhost:3835/SupportingMVCProject/PayUp?QN=ABC-1234567&PN=ABC1234567&AC=123456789. Il lance la page très bien et tous les contrôles sont interactifs, tous sauf mon "Pay Up!" bouton (lnkSubmit). Pourquoi lnkSubmit_Click dans Pay.aspx.cs n'est pas invoqué?
Votre point de vue est grandement apprécié!
je suis arrivé plus loin en supprimer les attributs suivants de la balise