Mr A
Mr A

Reputation: 6768

How to trigger certain methods on different aspx page using usercontrol

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.

Upvotes: 0

Views: 1101

Answers (2)

kerrubin
kerrubin

Reputation: 1616

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
}

Upvotes: 2

Beytan Kurt
Beytan Kurt

Reputation: 2263

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

Upvotes: 0

Related Questions