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

发布于 2024-10-20 15:42:47 字数 5609 浏览 1 评论 0原文

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

请参阅下面的代码:

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 页面上还有另一种方法。我想当客户端位于该页面时触发该方法。用户控件应根据用户当前所在的页面触发正确的方法。

I have 3 ASPX pages. A usercontrol on each page has an email placeholder in it. What I want to achieve is when the customer clicks on the usercontrol email placeholder, certain methods on a particular page should trigger.

See below for the code:

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);
    }
}

Also I have got another method on a different ASPX page. I want to trigger that method when the client is on that page. The user control should trigger the right method depending on the page user is currently on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-10-27 15:42:47

如果我理解得很好的话,你有 3 个 aspx 页面,只有一个包含表单的用户控件。
当您单击用户控件的按钮时,您希望在包含用户控件的页面上启动一个方法。

在您的用户控件中,您可以添加一个新属性(委托:Action<>),如下所示:

// 在您的用户控件中

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
}

If I understand well, you've got 3 aspx pages and only one usercontrol containing a form.
When you click on the usercontrol's button, you want to launch a method on the page containing the usercontrol.

In your usercontrol, you can add a new property (a delegate : Action<>), like this :

// In your user control

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);
}

// Specific method in the aspx page : (a simple one for the example)

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
}
溺孤伤于心 2024-10-27 15:42:47

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

You can send an event from the user control simply when email is clicked and catch it within the aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文