为什么我的MVC项目中的ASP.NET Webform LinkBut​​ton不会调用其代码隐藏的OnClick事件处理程序?

时间:2017-04-11 18:13:55

标签: asp.net-mvc forms webforms linkbutton

我有一个以前运行的webforms项目,我必须包含在一个基于MVC的预先存在的解决方案中,其中一个表单中的LinkBut​​ton控件现在只能部分工作;它的JavaScript事件处理程序工作得非常好(下面由OnClientClick引用)但是C#代码隐藏(lnkSubmit_Click)没有被调用!为什么不?

我的LinkBut​​ton在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 > 

代码隐藏确实存在于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!
        }
    }
}

我的RouteConfig.cs看起来像:

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

上面确定的PayUpController.cs PayUpIndex路由如下所示,它确实运行:

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

我的起始网址为http://localhost:3835/SupportingMVCProject/PayUp?QN=ABC-1234567&PN=ABC1234567&AC=123456789。它启动页面很好,所有控件都是交互式的,除了我的&#34; Pay Up!&#34;按钮(lnkSubmit)。为什么没有调用Pay.aspx.cs中的lnkSubmit_Click?

非常感谢您的见解!

1 个答案:

答案 0 :(得分:0)

我很确定我已经阅读了某个地方,如果您使用OnClientClick,那么除非您进行一些更改,否则服务器端代码将无法运行

查看以下链接

OnclientClick and OnClick is not working at the same time?