如何使用usercontrol在不同的aspx页面上触发某些方法

时间:2011-03-03 12:09:17

标签: asp.net methods controls

我有3个ASPX页面。每个页面上的用户控件都有一个电子邮件占位符。我想要实现的是当客户点击usercontrol电子邮件占位符时,应触发特定页面上的某些方法。

请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Tangent.Breakingfree;

public partial class uc_ucBottomMenu : System.Web.UI.UserControl
{

    public string printUrl { get; set; }
    public string emailUrl { get; set; }
    public string audioUrl { get; set; }
    public bool myDaigram { get; set; }
    public bool toolbox { get; set; }

    public uc_ucBottomMenu() {

        myDaigram = true;
        toolbox = true;

    }

    public Action<object, EventArgs> MyEvent;
    protected void Page_Load(object sender, EventArgs e)
    {
        btnTriggerEvent.Click += new EventHandler(btnTriggerEvent_Click);
    }

    void btnTriggerEvent_Click(object sender, EventArgs e)
    {
        MyEvent(sender, e);
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptInclude("bottomMenu", "../js/jqDock.js");

        // bit of an oppositen as always late request from client
        // saves going through all pages!!!

        phDiagram.Visible = myDaigram;

        phToolbox.Visible = toolbox;

        if (string.IsNullOrEmpty(printUrl))
        {
            phPrint.Visible = false;
        }
        else
        {
            phPrint.Visible = true;
            hplPrint.Attributes.Add("href", printUrl);
            if (printUrl.IndexOf("javascript") == -1)
            {
                hplPrint.Attributes.Add("target", "_blank");
            }
        }

        if (string.IsNullOrEmpty(emailUrl))
        {
            // quick fix for lloyd to do video guides
            // string fileupload = "../client/pdf/test_test.pdf";
            phEmail.Visible = true;
         //   hplEmail.Attributes.Add("href", "mailto:"+ emailUrl+ "?subject=Planning Your Time Positively &body=Well done for all the effort you have put into using the strategy of planning your time positively &attachement="+ fileupload+"");
            hplEmail.Attributes.Add("attachment", "../client/pdf/test_test.pdf");
        }
        else
        {
            string fileupload = "../client/pdf/test_test.pdf";
            phEmail.Visible = true;
           // hplEmail.Attributes.Add("href",emailUrl);
            hplEmail.Attributes.Add("href", "mailto:" + emailUrl + "?subject=Planning Your Time Positively &body=Well done for all the effort you have put into using the strategy of planning your time positively. &attachment=" + fileupload + "");

        }

        if (string.IsNullOrEmpty(audioUrl))
        {
            phAudio.Visible = false;
        }
        else
        {
            phAudio.Visible = true;
            hplAudio.Attributes.Add("href", audioUrl);
        }
    }

    protected void imgBtnLogOut_Click(object sender, ImageClickEventArgs e)
    {
        Helpers.Logout();
    }
}

//ASPX page. I want to trigger this method when the email button is clicked, this method generated pdf

    protected void CreateSummaryPDF(object sender, EventArgs e)
    {
        // create all node chart images
          //==========Difficult Situation Chart============//

        WriteDifficultSituations(true);
        IDataReader dr1 = null;
         dr1 = LBM.ChartGetSlider(userID, LBM.Slider.DifficultSituations);
         Points obj1 = SortToYArray(dr1);
        ChartDifficultSituations.SaveImage(Server.MapPath("../images/charts/") + SessionID.ToString() + "_" + LBM.Node.DifficultSituations.ToString() + ".png", ChartImageFormat.Png);
        double a = obj1.maxVal;
        dr1.Dispose();
        //======================End========================//


        double[] check = { a };
        // set up pdf
        IPdfManager objPdf = new PdfManager();

        // Create empty document
        IPdfDocument objDoc = objPdf.OpenDocument(Server.MapPath("../client/pdf/Progress-summary.pdf"), Missing.Value);
        IPdfPage objPage = objDoc.Pages[1];
        IPdfFont objFont = objDoc.Fonts["Helvetica-Bold", Missing.Value];

        int[] arrX = { 80, 306, 80, 306, 80, 306, 80, 306 };
        int[] arrY = { 590, 590, 290, 290, 590, 590, 290, 290 };

        int i = 0;

        // loop nodes and place on PDF
        foreach (string name in Enum.GetNames(typeof(LBM.Node)))
        {

            // move onto the next page
            if (i > 3) {
                objPage = objDoc.Pages[2];
            }

            // add images
            IPdfImage objImage = objDoc.OpenImage(Server.MapPath("../images/charts/") + SessionID.ToString() + "_" + name + ".png", Missing.Value);

            // Add empty page. Page size is based on resolution and size of image
            float fWidth = objImage.Width * 72.0f / objImage.ResolutionX;
            float fHeight = objImage.Height * 72.0f / objImage.ResolutionY;

            // Create empty param object
            IPdfParam objParam = objPdf.CreateParam(Missing.Value);

            objParam["x"].Value = arrX[i];
            objParam["y"].Value = arrY[i] - fHeight;

            objPage.Canvas.DrawImage(objImage, objParam);

            objPage.Canvas.DrawText(name, "x=" + arrX[i] + ", y=" + (arrY[i]+50) + "; width=208; height=200; size=11; alignment=center; color=#ffffff", objFont);

            i++;

        } 
        String strFileName = objDoc.Save(Server.MapPath("../client/pdf/filledform.pdf"), true);
    }
}

此外,我在另一个ASPX页面上有另一种方法。我想在客户端在该页面上时触发该方法。用户控件应根据用户当前所在的页面触发正确的方法。

2 个答案:

答案 0 :(得分:2)

如果我理解得很清楚,你有3个aspx页面,只有一个包含表单的usercontrol。 单击usercontrol的按钮时,您希望在包含usercontrol的页面上启动方法。

在您的usercontrol中,您可以添加一个新属性(委托:Action&lt;&gt;),如下所示:

//在您的用户控件中

public Action<object, EventArgs> MyEvent;
protected void Page_Load(object sender, EventArgs e)
{
    // The button in the usercontrol
    btnTriggerEvent.Click += new EventHandler(btnTriggerEvent_Click);
}
// The click event for your button
void btnTriggerEvent_Click(object sender, EventArgs e)
{
    // Raise the event (Action<>) when the button is clicked
    MyEvent(sender, e);
}

// aspx页面中的特定方法:(示例中的一个简单方法)

protected void Page_Load(object sender, EventArgs e)
{
    // Trigger is the name of the usercontrol
    Trigger.MyEvent = TriggerDefault;
}
public void TriggerDefault(object sender, EventArgs e)
{
    // Do whatever you want here
    Response.Write("ABOUT PAGE !!!"); // or some other text
}

答案 1 :(得分:0)

您可以在单击电子邮件时从用户控件发送事件并在aspx中捕获它。