我有一個以前運作的webforms項目,我不得不包含在一個先前存在的基於MVC的解決方案中,並且其中有一個表單中的LinkButton控件現在只能部分工作;它的JavaScript事件處理程序工作得很好(由OnClientClick引用),但C#代碼隱藏(lnkSubmit_Click)未被調用!爲什麼不?爲什麼我的MVC項目中的ASP.NET Webform LinkButton不會調用其代碼隱藏的OnClick事件處理程序?
我的LinkButton的內線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?
您的洞察力非常感謝!
我進一步得到從Pay.aspx中的