0

我创建了一个 html5 画布和 javascript 签名板,我想将其实现为一个带有更新面板的 Web 用户控件来处理按钮点击。出于某种原因,如果我将更新面板和签名板控件直接添加到 aspx 文件中,则自动回发事件会起作用,但是当我将相同的代码放入 Web 用户控件时,字段(画布、按钮、div 等)会出现,但自动回发不再有效。在这两种情况下,我都将脚本管理器放在更新面板上方。

这是我的 ascx 代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signature_Pad.ascx.cs"     Inherits="Controls_Signature_Pad" %>

 <asp:UpdatePanel runat="server" ID="signatureUpdate"  updatemode="Conditional">
  <Triggers>
     <asp:AsyncPostBackTrigger controlid="signatureApprove" eventname="Click" />
     <asp:AsyncPostBackTrigger controlid="sigClear" eventname="Click" />
     <asp:AsyncPostBackTrigger controlid="sigCancel" eventname="Click" />
  </Triggers>
  <ContentTemplate>
        <fieldset id="SignatureFieldSet" runat="server" style=" border: 1 solid black;">
       <p><asp:Label ID="signatureTextLabel" AutoPostBack="true"  runat="server" Text=""></asp:Label></p>
        <div id="canvasDiv" style="height: 300px; border:0px solid #000000; ">
            <canvas id="canvasSignature"  style="border:1px solid #000000;"></canvas>
        </div>
        <div id="sigButtonDiv"  style=" border:0px solid #000000;">
            <br /><br />
            <asp:Button AutoPostBack="true" runat="server" OnClick="OnApprove" ID="signatureApprove" Text="Approve" />
            <asp:Button AutoPostBack="true" runat="server" OnClick="OnClear" ID="sigClear" Text="Clear" />
            <asp:Button AutoPostBack="true" runat="server" OnClick="OnCancel" ID="sigCancel" Text="Cancel" />
        </div> 
    </fieldset>
 </ContentTemplate>
</asp:UpdatePanel>

这是我背后的 ascx 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Signature_Pad : System.Web.UI.UserControl
{
private string signatureData;
private string signatureText;

public string SignatureData
{
    get { return signatureData; }
    set { signatureData = value; }
}
public string SignatureText
{
    get { return signatureText; }
    set { signatureText = value; }
}

public void OnApprove(object sender, EventArgs e)
{
    UtilityClass.showMessageBox("Approve Clicked", this);
    SignatureText = "Approved Clicked";
}
public void OnClear(object sender, EventArgs e)
{
    UtilityClass.showMessageBox("Clear Clicked", this);
    SignatureText = "Clear Clicked";
}
public void OnCancel(object sender, EventArgs e)
{
    UtilityClass.showMessageBox("Cancel Clicked", this);
    SignatureText = "Cancel Clicked";
}

public void Page_Load(object sender, EventArgs e)
{
    signatureTextLabel.Text = signatureText;
}
}

这是我的相关aspx:

...
<%@ Register TagPrefix="mjt" TagName="SignaturePad" Src="~/Controls/Signature_Pad.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" EnablePageMethods="true"></asp:ScriptManager>
 <mjt:SignaturePad ID="SignaturePad" runat="server" Visible="true" SignatureData="" SignatureText="Test Signature Test." />

...
4

1 回答 1

1

在后面的代码中添加事件处理程序解决了这个问题。这是我的代码:

该控件具有三个需要触发事件的按钮(Approve、Clear 和 Canceled)。

在 ascx.cs 中:

public event EventHandler Approved;
public event EventHandler Cleared;
public event EventHandler Canceled;

 protected void signatureApprove_clicked(object sender, EventArgs e)
{

    if(Approved != null)
    Approved(this, EventArgs.Empty);

}
protected void sigClear_clicked(object sender, EventArgs e)
{
        if (Cleared != null)
        Cleared(this, EventArgs.Empty);
}
protected void sigCancel_clicked(object sender, EventArgs e)
{
       if (Canceled != null)
       Canceled(this, EventArgs.Empty);
}

在 ascx 中:

 <asp:UpdatePanel runat="server" ID="signatureUpdate"  updatemode="Conditional">
  <Triggers>
     <asp:AsyncPostBackTrigger controlid="signatureApprove" eventname="Click" />
     <asp:AsyncPostBackTrigger controlid="sigClear" eventname="Click" />
     <asp:AsyncPostBackTrigger controlid="sigCancel" eventname="Click" />
  </Triggers>
     <ContentTemplate>
       <fieldset id="SignatureFieldSet" class="fieldSetStyle" >
        <p><asp:Label ID="signatureTextLabel" AutoPostBack="true"  runat="server" Text=""></asp:Label></p>
        <div id="canvasDiv" runat="server" >
            <canvas id="canvasSignature" class="canvasStyle"></canvas> 
        </div>
        <div id="sigButtonDiv">
        <br />
            <asp:Button  AutoPostBack="true" runat="server" OnClick="signatureApprove_clicked" ID="signatureApprove" Text="Approve" CssClass="buttonStyle" />
            <asp:Button  AutoPostBack="true" runat="server" OnClick="sigClear_clicked" ID="sigClear" Text="Clear" CssClass="buttonStyle"/>
            <asp:Button  AutoPostBack="true" runat="server" OnClick="sigCancel_clicked" ID="sigCancel" Text="Cancel" CssClass="buttonStyle"/>
        </div> 
      </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>

在 aspx 中:

<mjt:SignaturePad OnApproved="Signature_Approved"   OnCanceled="Signature_Canceled" OnCleared="Signature_Cleared" ID="SignaturePad" runat="server"   />

在 aspx.cs 中:

protected void Signature_Approved(object sender, EventArgs e)
{
    //Do signature approved action
}
protected void Signature_Canceled(object sender, EventArgs e)
{
    //Do signature cancel action
}
protected void Signature_Cleared(object sender, EventArgs e)
{
   //Do signature cleared action
}
于 2012-08-08T13:21:25.857 回答