0

I have a previously operational webforms project I had to include in a preexistent MVC-based solution and in it there's a LinkButton control inside a form that is now only partially working; its JavaScript event handler works perfectly fine (referenced below by OnClientClick) but the C# code-behind (lnkSubmit_Click) isn't being invoked! Why not?

My LinkButton's inside 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 > 

The code-behind does exist inside 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!
        }
    }
}

My RouteConfig.cs looks like:

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

The PayUpController.cs PayUpIndex route identified above looks like the following, and it does run:

    public ActionResult Index(string QN, string PN, string AC)
    {

        string result = "";
        if (!AccessValidation(PN, out result))
        {
            return RedirectToAction(result);
        }

        // blah blah

        ViewData["message"] = "QN=" + QN + "PN=" + PN + "AC=" + AC;

        return this.View("~/Views/PayUp/Pay.aspx");
    }

My start URL is http://localhost:3835/SupportingMVCProject/PayUp?QN=ABC-1234567&PN=ABC1234567&AC=123456789. It launches the page just fine and all controls are interactive, all except my "Pay Up!" button (lnkSubmit). Why is lnkSubmit_Click inside Pay.aspx.cs not being invoked?

Your insight is greatly appreciated!

ShieldOfSalvation
  • 1,297
  • 16
  • 32
  • I got further by removing the following attributes from the
    tag in Pay.aspx: action="~/Views/Home/Pay.aspx" method="post". Now clicking the button at least does invoke some code-behind, just not the lnkSubmit_Click eventhandler I wish! Instead, the Index function inside PayUpCOntroller.cs is what's being executed.
    – ShieldOfSalvation Apr 12 '17 at 19:34

1 Answers1

0

I am pretty sure I have read somewhere that if you use OnClientClick then server side code will not run unless you make some changes

Check out the below link

OnclientClick and OnClick is not working at the same time?

Community
  • 1
  • 1
krilovich
  • 3,475
  • 1
  • 23
  • 33
  • Well, the funny thing is everything works perfectly for that previously operational webforms project when it stands on its own; it's when I stick it inside the MVC solution and try to integrate it there that I lose the lnkSubmit_Click button functionality. I tried the solution given in the link you provided and it didn't improve my situation. The JavaScript code still runs client-side, but the server-side code-behind lnkSubmit_Click eventhandler is never invoked. The old Page_Load eventhandler works! Why won't this lnkSubmit_Click? – ShieldOfSalvation Apr 12 '17 at 12:36