在MVC项目中的webform上使用webform用户控件

时间:2010-02-26 18:37:20

标签: asp.net asp.net-mvc user-controls

我在单个web.forms页面上使用服务器控件。我必须在web.forms页面上使用此控件,因为它是一个服务器控件,尽管这实际上是一个MVC项目。所以我创建了一个web.forms文件夹并将我的新页面放入其中。然后,我从签名控件中复制示例代码。我收到以下错误:

The base class includes the field 'ctrlSign', but its type (WebSignatureCapture.SignatureControl) is not compatible with the type of control (ASP.signaturecapture_signaturecontrol_ctlsignature_ascx).

我知道代码有效,因为如果我从服务器控件中删除了ID属性,它就不再给我这个错误并且我的控件呈现了。但是我需要ID的属性,所以我可以执行的是post事件...任何想法为什么?

我正在使用 this 签名控件。这是web.forms代码......

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="KahunaCentralTIDRevamp.SignatureCapture.Index" %>

<%@ Reference Control="~/SignatureCapture/SignatureControl/ctlSignature.ascx" %>
<%@ Register TagPrefix="uc" TagName="Signature" Src="~/SignatureCapture/SignatureControl/ctlSignature.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
    <title>Signature Application Demo</title>
</head>
<body>

    <form id="frmOrder" method="post" runat="server">
    <div>
        Please Sign Below:
    </div>
    <div>
        <uc:Signature ID="ctrlSign" SignHeight="150" SignWidth="300" SignatureCodePath="~/SignatureCapture/SignatureControl/"
            SavePath="~/SignatureCapture/" SignatureFileFormat="Gif" runat="server" />
        <%--    <uc:Signature id="ctlMySignature" PenColor="Red" PenWidth="2" BackColor="Yellow" SignWidth="300" SignHeight="150"
                            SavePath="~/Signatures/" SignatureCodePath="~/SignatureControl/" SignatureFileFormat="Gif" Runat="server"></uc:Signature>--%>
    </div>
    <div>
        <input type="button" value="  Re-Sign " onclick="ClearSignature();">
        <asp:Button runat="server" ID="btnSave" Text=" Save " onmousedown="document.getElementById('btnSave').value = 'Wait...';"
            OnClientClick="DirectSave();" OnClick="btnSave_Click" />
    </div>
    </form>

    <script language="javascript" type="text/javascript">
        // This is the method that is directly called, this will save signature
        // and then call server code to do further processing. You can change
        // the delay of 5 seconds as per your needs
        function DirectSave() {
            SaveSignature();

            var date = new Date();
            var curDate = null;

            // delay of 5 seconds, 5000 milisecons, change as per requirement
            do { curDate = new Date(); }
            while (curDate - date < 5000);

            return true;
        }
    </script>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

打开用户控件的.ascx标记文件。它应该是这样的:

<%@ Control 
    Language="C#" 
    AutoEventWireup="true" 
    CodeFile="ctlSignature.ascx.cs" 
    Inherits="WebSignatureCapture.SignatureControl.ctlSignature" %>

将其修改为:

<%@ Control 
    Language="C#" 
    AutoEventWireup="true" 
    CodeBehind="ctlSignature.ascx.cs" 
    Inherits="WebSignatureCapture.SignatureControl.ctlSignature" %>

注意CodeFile - &gt;代码隐藏。

答案 1 :(得分:0)

我认识的人有一段时间遇到了类似的问题,然后他们发现了一些可以在BeginRequest中做某事的事情,这些事情对他的问题进行了分类并允许他在视图中使用服务器控件。我快速搜索了它,我相信this就是他所使用的。

以下代码:

void Application_BeginRequest(object sender, EventArgs e)
{
  var form = HttpContext.Current.Request.Form;

  form.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(form, false, null);

  // Refinement 1:
  foreach (var key in form.AllKeys.Where(key => key.Contains("$")))
  { 
    var value = formkey;
    form.Remove(key);
    var newKey = key.Substring(key.LastIndexOf("$") + 1); 
    form.Add(newKey, value);
  }
}