ASP.NET更新面板无法在Web用户控件内部工作

时间:2012-08-03 18:49:34

标签: c# asp.net web-user-controls updatepanel

我创建了一个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." />

...

1 个答案:

答案 0 :(得分:1)

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

控件有三个按钮需要触发事件(批准,清除和取消)。

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