如何使用用户控件在不同的aspx页面上触发某些方法
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解得很好的话,你有 3 个 aspx 页面,只有一个包含表单的用户控件。
当您单击用户控件的按钮时,您希望在包含用户控件的页面上启动一个方法。
在您的用户控件中,您可以添加一个新属性(委托:Action<>),如下所示:
// 在您的用户控件中
// aspx 页面中的特定方法:(示例中一个简单的方法)
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
// Specific method in the aspx page : (a simple one for the example)
您可以在单击电子邮件时从用户控件发送一个事件,并在 aspx 中捕获它。
You can send an event from the user control simply when email is clicked and catch it within the aspx.