2017-04-11 30 views
0

我有一個以前運作的webforms項目,我不得不包含在一個先前存在的基於MVC的解決方案中,並且其中有一個表單中的LinkBut​​ton控件現在只能部分工作;它的JavaScript事件處理程序工作得很好(由OnClientClick引用),但C#代碼隱藏(lnkSubmit_Click)未被調用!爲什麼不?爲什麼我的MVC項目中的ASP.NET Webform LinkBut​​ton不會調用其代碼隱藏的OnClick事件處理程序?

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

我的起始URL是http://localhost:3835/SupportingMVCProject/PayUp?QN=ABC-1234567&PN=ABC1234567&AC=123456789。它啓動頁面就好了,所有的控件都是互動的,除了我的「支付!」之外。按鈕(lnkSubmit)。爲什麼沒有調用Pay.aspx.cs中的lnkSubmit_Click?

您的洞察力非常感謝!

+0

我進一步得到從Pay.aspx中的

標記中刪除以下屬性:action =「〜/ Views/Home/Pay.aspx」method =「post」。現在單擊按鈕至少會調用一些代碼隱藏功能,而不是我希望的lnkSubmit_Click事件處理程序!相反,PayUpCOntroller.cs中的Index函數就是執行的內容。 – ShieldOfSalvation

回答

0

我敢肯定我讀的地方,如果你使用的OnClientClick那麼服務器端代碼將不會運行,除非你做出一些改變

退房下面的鏈接

OnclientClick and OnClick is not working at the same time?

+0

那麼,有趣的是,當它獨立時,一切都完美地適用於以前運營的webforms項目;當我將它粘在MVC解決方案中並嘗試將其集成在那裏時,我失去了lnkSubmit_Click按鈕功能。我嘗試了您提供的鏈接中提供的解決方案,但並未改善我的情況。 JavaScript代碼仍然運行在客戶端,但從不調用服務器端代碼隱藏lnkSubmit_Click事件處理程序。舊的Page_Load事件處理程序正常工作!爲什麼不會這個lnkSubmit_Click? – ShieldOfSalvation

相關問題