There's more than question and article about the same exact question but I have a couple more related questions and was hoping to get some answers.
I've heard of two approaches to find the button and add the handler or use an interface (Check both approaches from here) .. Which one do you suggest ?
If you could please illustrate the 'Interface' option with some code and where to class the interface file cause it's not readable in the page when I try to inherit it!
Second aproach is IMO better. The first choice couples a page to the specific master page, and it is not nice.
All files are placed in the same folder.
IPageInterface.cs:
namespace CallFromMasterPage
{
public interface IPageInterface
{
void DoSomeAction();
}
}
Default.aspx.cs:
namespace CallFromMasterPage
{
public partial class Default : System.Web.UI.Page, IPageInterface
{
public void DoSomeAction()
{
throw new NotImplementedException();
}
}
}
Site.Master.cs:
namespace CallFromMasterPage
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Button1_Click(object sender, EventArgs e)
{
IPageInterface pageInterface = Page as IPageInterface;
if (pageInterface != null)
{
pageInterface.DoSomeAction();
}
}
}
}
There are other approaches. E.g. you can publish an event via event broker.
See From My Openion it will be best if you make use of event handlers...and even with the custom delegate..
like this
public delegate ReturnType MasterPageButtonHandler(CustomEventArgs ObjPriargs);
public event MasterPageButtonHandler MasterPagebuttonClick;
.
.
.
.
Button.click+=new EventHandler(Button1_Click);
.
.
.
protected void Button1_Click(Object sender,EventArgs e)
{
if(MasterPagebuttonClick!=null)
{
CustomEventArgs ObjPriargs=new CustomEventArgs();
ObjPriargs.Property1=SomeValu1;
ObjPriargs.Property2=SomeValu2;
MasterPagebuttonClick.Invoke(ObjPriargs);
}
}
.
.
.
public class CustomEventArgs
{
Public DataType Property1{get;set;}
Public DataType Property2{get;set;}
Public DataType Property3{get;set;}
}
.
.
.
// Now in your aspx Page
MyMaster m=Page.Master as MyMaster;
m.MasterPagebuttonClick+=new MasterPageButtonHandler(MasterPageHandler_Click);
.
.
.
protected void MasterPageHandler_Click(CustomEventArgs ObjPriargs)
{
//You code/////
}
going through this manner give some flexibility like in case if in future you want to pass some data tour content page when clicked..its easily possible.
I have a calendar in my MasterPage, but I need to use this date to bind a datagrid in a ContentPage.
MasterPage html:
<asp:Calendar ID="calAgenda" DefaultView="Days" Format="dd/MM/yyyy" runat="server" BackColor="White" CssClass="templeteCalendar" Visible="false" OnSelectionChanged="calAgenda_SelectionChanged"></asp:Calendar>
ContentPage html:
<asp:TextBox runat="server" ID="txtFechaBusqueda" CssClass="form-control templeteLabel" MaxLength="10" />
MasterPage C# Code:
protected void calAgenda_SelectionChanged(object sender, EventArgs e)
{
TextBox txtCalendarDate = (TextBox)ContentPlaceHolder1.FindControl("txtFechaBusqueda");
txtCalendarDate.Text = calAgenda.SelectedDate.ToString().Substring(0, 10);
}
Here We have use a EventHandler in the ContentPage to catch the event click in the MaserPage.
ContentPage C# Code:
Calendar btnCalendar = Master.FindControl("calAgenda") as Calendar;
btnCalendar.SelectionChanged += new EventHandler(btnCalendar_Click);
Then We need to define the btnCalendar_Click function.
protected void btnCalendar_Click(object sender, EventArgs e)
{
DoSomething();
}
Related
In an ASP.Net project, using C#, I have a class (PlcComms.cs) for talking to a Controller (a PLC).
I want the one class object to be globally available, for each web page in my project.
I see from reading various forum posts that I should be able to create a public static object of my class type, i.e. PlcComms, in Global.asax.cs, or in a class in the App_Code folder. I've tried both and I write to the object ok, but when I go to read from it (from a timer in an update panel on the home web page) then it always read back as null.
I'm at a loss to know what to do at this point. can anyone help?
Currently, this is a class I have in the App_Code folder...
namespace SpearheadWeb
{
public static class AppGlobal
{
public static SpearheadWeb.PlcComms PlcCommsObject { get; set; }
}
}
this I have on my web page - it seems to create the object OK...
namespace SpearheadWeb
{
public partial class _Default : Page
{
private PlcComms CurrentPLC;
//some other here including ComPorts
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
//some settings reading and setup here
CurrentPLC = new PlcComms(Global.CommsSettings.CpuType1,Global.CommsSettings.Network1,ComPorts[0], Global.CommsSettings.IPAddress1, Global.CommsSettings.Path1,UpdatePanel.Controls[0].Controls, 1, Global.CommsSettings.MsUpdate1);
AppGlobal.PlcCommsObject = CurrentPLC;
but in my timer (the timer within an updatepanel) PLCComms1 is always null here...
protected void TimerUpdate_Tick(object sender, EventArgs e)
{
PlcComms PLCComms1 = AppGlobal.PlcCommsObject;
I've only recently started to develop in ASP.NET, though I've been using C# for several years now. My first approach to ASP.NET was using MVC 4, but now I find myself working on a project that uses plain ASP.NET pages. I'm at a loss as to how the general framework works.
Specifically, I have no idea how to implement the Decorator Pattern in an ASP.NET page. I have a Product.aspx page and I have to add a feature to it. I thought that Decorator Pattern would be best based on the task requirements, and I immediately figured out how I would use it in MVC, since the actual logic that is executed lies in the Controller Action: there I would instantiate my decorator object.
But I have no idea how to do it in ASP.NET. As far as I can see, when the browser requests Product.aspx "something" creates an object of class Product (derived from Page), and then it's too late to decorate it.
Is it therefore possible to decorate a whole ASP.NET page (not just an object used by the code behind)? How would I do that?
I am not exactly sure what you wanna decorate, but;
You can create an HttpHandler that lets you do your work on a particular request as follows
public class MyHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.RawUrl.Contains("product.aspx"))
{
// may be you can execute your decorate business here
}
}
public bool IsReusable { get { return false; } }
}
Or may be you can use Global.asax 's OnBeginRequest event like as follows
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterOpenAuth();
RouteConfig.RegisterRoutes(RouteTable.Routes);
base.BeginRequest += OnBeginRequest;
}
private void OnBeginRequest(object sender, EventArgs e)
{
if (Request.RawUrl.Contains("product.aspx"))
{
//execute your business here..
}
}
First of all, according to your comment below the question, I can say that you should implement your showcase as a web user control (ascx)
You can decorate your ShowCase as follows,
public partial class ShowCase : System.Web.UI.UserControl, IShowCase
{
protected void Page_Load(object sender, EventArgs e){}
public void ApplyConfiguration(IConfiguration configuration)
{
throw new NotImplementedException();
}
}
public interface IShowCase
{
void ApplyConfiguration(IConfiguration configuration);
}
public abstract class Decorator : IShowCase
{
protected IShowCase ShowCase;
protected Decorator(IShowCase showcase)
{
ShowCase = showcase;
}
public virtual void ApplyConfiguration(IConfiguration configuration)
{
ShowCase.ApplyConfiguration(configuration);
}
}
public class ShowCaseDecoratorA : Decorator
{
public ShowCaseDecoratorA(IShowCase showcase) : base(showcase){ }
public override void ApplyConfiguration(IConfiguration configuration)
{
base.ApplyConfiguration(configuration);
//depending on the configuration, do something..
ShowCase.Visible = false;
}
}
public interface IConfiguration
{
//configuration
}
Then, from inside the page that uses ShowCase user control, you do something like this,
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
IConfiguration configuration = ConfigurationFactory.Get();
new ShowCaseDecoratorA(this.ShowCase).ApplyConfiguration(configuration);
}
}
I hope this gives you some inspiration..
I know in C# I can make a factory but I don't not know how to reuse code in aspx. My code was originally purposed for ARList only, but now has IcnList. I thought about making a switch statement but isn't there something better to code?
The Function
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ARExtractionController arController = new ARExtractionController();
Dictionary<int, string> ARDictionary = arController.GetTickets();
List<int> sortedARList = new List<int>();
foreach (KeyValuePair<int, string> kv in ARDictionary)
{
sortedARList.Add(kv.Key);
}
sortedARList.Sort();
sortedARList.Reverse();
ARList.DataSource = sortedARList;
ARList.DataBind();
ARList.Items.Insert(0, " ");
ARList.AutoPostBack = true;
}
}
If you just want to be able to reuse the code, add a class to your project for utility methods and dump it in there for both pages to use.
If you're trying to reuse code with and associated UI, look into User Controls (ascx files) which allow you to do just that.
It isn't clear which is right for you from the question.
In TicketExtractionWeb, create a BasePage class.
BasePage.cs:
public class BasePage : System.Web.UI.Page
{
// Add methods that are used on all your pages in here.
protected DateTime GetCurrentDate()
{
return DateTime.Now;
}
}
And a page (we'll call it MyPage):
public class MyPage : BasePage
{
protected Page_Load(object sender, EventArgs e)
{
var currentDate = this.GetCurrentDate();
}
}
So when you create another aspx page, it will default to:
public class MyNewPage : System.Web.UI.Page
{
// ...
}
Just change : System.Web.UI.Page to : BasePage
You should be able to inherit from a single page to reuse the code.
Top of aspx file:
<%# Page Language="C#" AutoEventWireup="true" Inherits="MyNamespace.CommonCode" %>
If you want to reuse html , please use a user control file (.ascx) , which can be reused muliple times
when do you need .ascx files and how would you use them?
I'm looking for a way to add some code to execute in all Page_Load events on all pages of my web application, without have to write it in all the pages.
The code must execute before the Page_Load methods on the pages.
Thanks for the attention.
You can create one Class let say BasePage.cs, and here you will have one virtual method Page_Load:
public class BasePage: System.Web.UI.Page
{
protected virtual void Page_Load(object sender, EventArgs e)
{
//Some logic here that you want to execute for all pages
}
}
Then, in every page where you want to execute this code on PageLoad, make that page to inherit from BasePage and override the PageLoad method, like this:
in file somePage.aspx.cs do this:
public partial class somePage : BasePage
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e); //This line will execute page load from BasePage class
//The rest of code you want to execute on this page load
}
}
You could create a master page, set all the pages you want the code to be executed as "childs" of your master page, and them put the code you want to be executed on the Page_Load event of your master page.
To see how master pages work: http://msdn.microsoft.com/en-us/library/ie/wtxbf3hh.aspx
I'm trying to figure out a way that I can call a Web Method (located as a public static method in the code behind of an aspx page) from another ASPX Code Behind page.
This is the code behind of Page A.aspx
public partial class PageA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// other code not relevant is here
}
[WebMethod(true)]
public static string GetStringInfo()
{
// do some stuff here to build my string
// return string
}
}
On page B, I need to be able to call GetStringInfo() during page load or some other event to get the information. the GetStringInfo() is fairly complex and for reasons outside of my control, can't be moved elsewhere or rebuilt presently.
How can I consume the web method above from another page's code behind?
I've tried instantiating a copy of the other page (PageB), such as:
public partial class PageB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PageA page = new PageA();
page.GetStringInfo();
}
}
The problem here is since it's dynamically compiled, I don't have an easy namespace to reference and access. I've tried adding one, and it ignores it.
This project is on .net 3.5, C#, and is a web site project (not a web application).
Any help is appreciated!
If the GetStringInfo method is static you don't need an instance of PageA to invoke it:
public partial class PageB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string info = PageA.GetStringInfo();
}
}