Apart from LogChange is there a more specific event that gets fired when adding/deleting a related page to a document ?
I want to be able to remove a page from cache when this happens
Have you tried using the global events defined by the CMS.DocumentEngine.RelationshipInfo.TYPEINFO.Events property? For example:
public class CustomRelationshipEvents : CMSLoaderAttribute
{
public override void Init()
{
RelationshipInfo.TYPEINFO.Events.Insert.After += Insert_After;
}
private void Insert_After(object sender, ObjectEventArgs e)
{
// Clear cache here
}
}
Then add your CustomRelationshipEvents attribute to an extension of Kentico's CMSModuleLoader class...
Related
I want a process to be called each time I navigated to my view to refresh a list.
I am using Xamarin Forms and prism framework.
I made my ViewModel derivate from ContentPage but the following method is never called :
protected override void OnAppearing()
{
//Do things
}
How am I supposed to do to get the event? Is it better to use OnNavigateTo?
Through the document:
There are times in your application where you may want to invoke code
in your ViewModel based on when the Page Appears or Disappears without
Navigation specific consideration. For these times you can utilize the
IPageLifecycleAware interface to properly respond to the Appearing and
Disappearing events from the Page.
public class ViewAViewModel : IPageLifecycleAware
{
public void OnAppearing()
{
Console.WriteLine("We are appearing");
}
public void OnDisappearing()
{
Console.WriteLine("We are disappearing");
}
}
I found a solution to make my code work, I add to do this in my code behind from the page:
protected override void OnAppearing()
{
(BindingContext as IPageLifecycleAware)?.OnAppearing();
}
Still a mystery why I need to add this and it is not in the sample.
at the time of this post, the sample does it differently. It uses Behaviors to achieve expectation
It has a PageLifeCycleAwareBehavior class
private void OnAppearing(object sender, EventArgs e)
{
MvvmHelpers.InvokeViewAndViewModelAction<IPageLifecycleAware>(AssociatedObject, aware => aware.OnAppearing());
}
private void OnDisappearing(object sender, EventArgs e)
{
MvvmHelpers.InvokeViewAndViewModelAction<IPageLifecycleAware>(AssociatedObject, aware => aware.OnDisappearing());
}
You can see the full implementation here
You can also call Initialize is a good alternative
Add ini to your class
public class HelloViewModel : BindableBase, IInitialize
then add the following method
public void Initialize(INavigationParameters parameters)
{
// Do stuff
}
I am trying to convert an ASP.net web application into a website. One issue i am coming across is trying to access usercontrols and masterpages in my code files.
The code below shows a class i am using for a base class, it inherits from System.Web.UI.Page.
public class AdminBase : BasePage
{
DeniedAccess deniedAccessControl;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//All pages that inherit from this will have a denied access control which will display when an admin of one instance tries to access admin pages of a different instance
//which they don't have access to. This enables us to still use the built in web.config authroisation API
deniedAccessControl = (DeniedAccess)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");
((MasterPage1)Master).MainContent.Controls.Add(deniedAccessControl);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (deniedAccessControl.ContentVisible)
{
foreach (Control control in ((MasterPage1)Master).MainContent.Controls)
{
//only sets to false if the control is set to true as it will error if you try to set a non visiual controls "Visible" property. E.g an sqldatasource control
if (control.GetType() != deniedAccessControl.GetType() && control.Visible)
control.Visible = false;
}
}
}
}
The "DeniedAccess" control is a user control i am dynamically adding if the user fails some custom authorization.
This worked fine when this was a web application as the DeniedAccess control was compiled into the same dll so i could access it from the code. Now that it is a website it cannot find the namespace/class as it is not in the App_Code folder.
Is what I'm trying to do possible in a website project? I can't seem to find a way and the only alternative i see is having to write this code for each individual page rather than use it on a base page.
I also get the same problem when trying to cast the master page to my "MasterPage1" class for the same reason, it can't find it so i cannot access it's properties.
I ended up creating two interfaces to solve this problem, as it wasn't a code behind file i couldn't just add a reference to the control/master page in the source.
I made my usercontrol inherit this interface
public interface IDeniedAccessControl
{
bool ContentVisible
{
get;
}
}
And my master page inherit this one
public interface IMasterPage
{
void AddControl(Control control);
ControlCollection MainContentControls
{
get;
}
}
I then made my usercontrol and master page implement there respective interface with the desired functionality.
Master page implementation:
public void AddControl(Control control)
{
ContentPlaceHolder1.Controls.Add(control);
}
public ControlCollection MainContentControls
{
get { return ContentPlaceHolder1.Controls; }
}
User control implementation:
bool IDeniedAccessControl.ContentVisible
{
get { return ContentASPxPanel.Visible; }
}
I could then use this on any code file. Below is how i edited my original code.
public class AdminBase : BasePage
{
IDeniedAccessControl deniedAccessControl;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//All pages that inherit from this will have a denied access control which will display when an admin of one instance tries to access admin pages of a different instance
//which they don't have access to. This enables us to still use the built in web.config authroisation API
deniedAccessControl = (IDeniedAccessControl)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");
((IMasterPage)Master).AddControl((Control)deniedAccessControl);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (deniedAccessControl.ContentVisible)
{
foreach (Control control in ((IMasterPage)Master).MainContentControls)
{
//only sets to false if the control is set to true as it will error if you try to set a non visiual controls "Visible" property. E.g an sqldatasource control
if (control.GetType() != deniedAccessControl.GetType() && control.Visible)
control.Visible = false;
}
}
}
}
I've got a bit of an issue with creating a new control based on ASP.NET's ImageButton control. Everything works as expected, except for the click handler that is being hooked up in the control's OnInit override. Basically, clicking the custom image button just refreshes the page, never hitting the handler.
Now, I know this is something stupid I've done or just not understood, but I can't for the life of me figure this out. All the articles, questions and forum posts I've found on event handling issues for controls is for child controls, rather than ones that inherit from existing control types and have their own predefined handlers.
The following code is what I've written:
public class WebPaymentButton : ImageButton
{
public string DisabledImageUrl { get; set; }
public string TermsAcceptClass { get; set; }
protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterClientScriptResource(typeof (WebPaymentButton), "PaymentModule.Scripts.WebPaymentButtonScript.js");
}
protected override void OnInit(EventArgs e)
{
CssClass = "WebPaymentButton";
if (!string.IsNullOrWhiteSpace(TermsAcceptClass))
{
Attributes["data-TermsClass"] = TermsAcceptClass;
}
if (!string.IsNullOrWhiteSpace(DisabledImageUrl))
{
Attributes["data-DisabledImageUrl"] = ResolveUrl(DisabledImageUrl);
}
Click += WebPaymentButton_Click;
base.OnInit(e);
}
private void WebPaymentButton_Click(object sender, ImageClickEventArgs e)
{
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}
}
I've tried hooking the handler up in the OnLoad and also switching it to run after the base.OnInit/OnLoad calls. Nothing has solved the handler issue. Can anyone point me in the right direction?
In case it helps, here is the markup for the button on the page:
<pm:WebPaymentButton runat="server" ImageUrl="~/pay-now.png" DisabledImageUrl="~/not-pay-now.png" TermsAcceptClass="TermsCheckbox" ID="MainPayButton" />
Have you tried overriding the OnClick event handler instead of hooking up to a new event handler?
Remove the Click += WebPaymentButton_Click line from OnInit and remove the WebPaymentButton_Click function, then add the following code to your class instead:
protected override void OnClick(ImageClickEventArgs e)
{
base.OnClick(e);
HttpContext.Current.Response.Redirect("http://dummy_payment_page_in_place_of_code", true);
}
I want to check Session in some pages. To do this I am adding the page names which I want to check inside web.config as a appsetting key.
I want to use httpHandler with firing an event after it finds the session is empty or something else.
If I create httpHandler as a dll(another project) and add to a web site, can handler fire an event and web site capture it inside a web page?
What you can do is this:
Your HttpHandler puts a value in the HttpContext.Current.Items collection telling if there was Session or not. Something like
HttpContext.Current.Items.Add("SessionWasThere") = true;
You create a BasePage that checks that value in the Page_Load event and raises a new event telling so:
public abstract class BasePage : Page {
public event EventHandler NoSession;
protected override void OnLoad(EventArgs e){
var sessionWasThere = (bool)HttpContext.Current.Items.Add("SessionWasThere");
if(!sessionWasThere && NoSession != null)
NoSession(this, EventArgs.Empty);
}
}
In your page, you suscribe to that event:
public class MyPage : BasePage{
protected override void OnInit(){
NoSession += Page_NoSession;
}
private void Page_NoSession(object sender, EventArgs e) {
//...
}
}
I'm working on a project where performance is really important and i would like to disable AutoEventWireup permanently. The problem is in the fact that the page and control directives override the settings from web.config. Since there will be many collaborators on this project it will be impossible to expect everyone to disable it when creating new pages and controls (and it is true by default). Is it possible to somehow enforce this rule?
I hoped to solve this in a similar way as I did with the ViewState - setting the EnableViewState to false programmatically in the base page that all new pages inherit. But it seems there is no equivalent for AutoEventWireup.
Override SupportAutoEvents
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
this.Init += (_o, _e) => {
this.Load += (o, e) => {
Response.Write("in page handler");
};
};
}
protected void Page_Load(object sender, EventArgs e)
{
// this handler is NOT assigned to the Load event
Response.Write("assigned by ASP .Net handler");
}
protected sealed override bool SupportAutoEvents
{
get
{
return false;
}
}
}
Create your own set of controls for each of the out of the box ones that you want to disable these properties for. Then set the two controls in the inherited versions to readonly.