Calling a base page method from a user control in asp.net - c#

I have been trying to find a good answer to this question, but can't seem to find one. I have an ASP.NET page that derives from a base page, like this:
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This works fine
}
}
And the base page:
public partial class MyBasePage: Page
{
}
protected DateTime GetLoginTime()
{
// Do stuff
return loginTime;
}
Now I have a user control on that page that needs to call my method...Like this:
public partial class TimeClock : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This does not work!
}
}
As you can see, I cannot call my base method, for obvious reasons. My question is, how can I call this method from my user control? One work around I've found is like this:
var page = Parent as MyBasePage;
page.GetLoginTime(); // This works IF I make GetLoginTime() a public method
This works, if I make my function public instead of protected. Doing this doesn't seem like a very OOP way to tackle this solution, so if someone can offer me a better solution, I'd appreciate it!

TimeClock inherits from UserControl, not from MyBasePage so why should TimeClock see the Method GetLoginTime()?

You should keep your UserControl out of your Page stuff. It should be decoupled in OOP speak. Add properties to set values and delegates to hook into events:
public partial class TimeClock : UserControl
{
public DateTime LoginTime{ get; set; }
public event UserControlActionHandler ActionEvent;
public delegate void UserControlActionHandler (object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.ActionEvent!= null)
{
this.ActionEvent(sender, e);
}
}
}
Page
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime();
TimeClock1.LoginTime = loginTime;
TimeClock1.ActionEvent += [tab][tab]...
}
}

(this.Page as BasePage).MethodName()

Related

Get Public Content Page Variable into Masterpage

I have a public variable in my content page that I need to access in my MasterPage. So I can set a Javascript variable... .
How can I reference a public content page variable from the masterpage?.
I supposed you want to say that you want to access to variable in the MasterPage from Contend Page, if is correct, use this example:
Declare your public or protected variable:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string strEmpresa = "NS";
protected void Page_Load(object sender, EventArgs e)
{
}
}
Set the following directive at the beginning of your content page:
<%# MasterType virtualPath="~/MasterPage.Master"%>
then you can use the public variables of your MasterPage, using Master.NameVariable.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = Master.strEmpresa;
}
}
In other case if you really want access to variable in ContentPage from MasterPage, you just can set the value in Session and then read in MasterPage. For example:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myVariable"] != null)
{
TextBox1.Text = Session["myVariable"].ToString();
}
}
}
}
public partial class WebFormMP_TestPublicVariable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["myVariable"] = "Test";
}
}
}
There are many ways that you can achieve this. check around internet ;).

Modified UserControl

I have following control class withsome extra methods in it. I have multiple form usercontrols, FormChoosePage has dropdownlist with form list in it, and I get selected forms user control, load its data and after he click save button I call forms save method.
at btnSave_Click ucForm is null, I have 2 question;
1 ) how can I keep dynamicly Modified UserControl (View State or something?)
2 ) am I on right way to do this with generic user controls and everything. If not, what is you sugestion?
FormControler
public class Controler : UserControl
{
public virtual void PageLoad() { }
public virtual void SaveForm() { }
}
Form UC
public partial class ApplicationForm : Controls.Controler
{
public override void PageLoad()
{
ddlFormType.DataSource = [Data];
ddlFormType.DataBind();
}
public override void SaveForm()
{
XForm form = new XForm();
form.something = txtSomething.Text;
form.Save();
}
}
Form Choose Page
public partial class FormChoosePage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlFormLoad();
}
}
Controler ucForm;
protected void ddlForm_SelectedIndexChanged(object sender, EventArgs e)
{
XForm form = XForm.Get<XForm>(ddlForm.SelectedValue);
ucForm = this.LoadControl(form.URL) as Controler;
ucForm.ID = "ucForm";
ucForm.PageLoad();
}
protected void btnSave_Click(object sender, EventArgs e)
{
ucForm.SaveForm();
}
}

How to Implement Interface Functions in User Control

Hi I'm creating test application that has a user control with a Button that will be reference in a Form.
Is it possible to bind interface
public interface ICRUD
{
void Test();
}
to the user Control button1 click event
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//CALL ICRUD.Test() execute when click on form1 and then show I am Clicked
}
}
so that
i just need to implement only the interface functions to my form1.
Form1:
using System.Windows.Forms;
namespace TEST
{
public partial class Form1 : Form , ICRUD
{
public Form1()
{
InitializeComponent();
}
public void Test()
{
MessageBox.Show("I am Clicked");
}
}
}
Thanks in Regards.
This doesn't seems right to me. It's probably not the Form that should be implementing this interface. What's more, the interface doesn't bring anything here.
But if you really want to do that, you can access the ParentForm property, cast it to your interface and then call the method:
private void button1_Click(object sender, EventArgs e)
{
var crud = (ICrud)ParentForm;
crud.Test();
}
Also, the convention in .Net is to write abbreviations (at least the long ones) the same as other words, so you should name your interface ICrud.
private void button1_Click(object sender, EventArgs e)
{
for(var parent = this.Parent; parent != null; parent = parent.Parent)
{
var crud = parent as ICRUD;
if (crud != null)
{
crud.Test();
break;
}
}
}

Subscribing to an event of a ascx of another ascx in a different window

I'm trying to subscribe to the the save button event of a user control that is launched in a separate radwindow from the calling parent. but I am getting object not initialized error, I know why but what am I missing?
Update: I found my error but it appears that if (this.SaveEvent!= null) in the ControlBase is always null
Parent Control Code:
public partial class myControl : ControlBase
{
private myChildControl __myChildControl;
private void myControl_PreRender(object sender, EventArgs e)
{
// error occurs here
//this.__myChildControl.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
// found my error
this.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
}
private void __myChildControl _SaveEvent(object sender, CustomEventArgs e)
{
this.Label1.Text = e.CustomEventArg1.ToString();
this.Label2.Text = e.CustomEventArg2.ToString();
}
}
Child Control Launched in RadWindow:
public partial class myChildControl : ControlBase
{
protected void btnSave_OnClick(object sender, EventArgs e)
{
CustomEventArgs _cea = new CustomEventArgs {CustomEventArg1 = 123, CustomEventArg2 = 12};
callBaseMethod(_cea);
}
}
ControlBase Code:
public class ControlBase : UserControl
{
public event CustomEventHandler SaveEvent;
public delegate void CustomEventHandler(object sender, CustomEventArgs e);
internal void callBaseMethod(CustomEventArgs cea)
{
if (this.SaveEvent!= null)
{
this.SaveEvent(this, cea);
}
}
}
CustomEventArgs class:
public class CustomEventArgs : EventArgs
{
public int CustomEventArgs1 { get; set; }
public int CustomEventArgs2 { get; set; }
}
This isn't possible in codebehind: the RadWindow presents a separate aspx/ascx page altogether that is linked to the main page through javascript alone.
What you need to do is handle the RadWindow OnClientClose event in javascript, then fire something in the parent page that performs the appropriate tasks.

How to Execute Page_Load() in Page's Base Class?

I have the following PerformanceFactsheet.aspx.cs page class
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
where FactsheetBase is defined as
public class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
The problem is that FactsheetBase's Page_Load is not executing.
Can anyone tell me what I'm doing wrong? Is there a better way to get the result I'm after?
Thanks
We faced the similar problem, All you need to do is just register the handler in the constructor. :)
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
this.Load += new EventHandler(this.Page_Load);
}
public MyPageData Data { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
Another approach would be to override OnLoad() which is less preferred.
public class FactsheetBase : System.Web.UI.Page
{
public FactsheetBase()
{
}
public MyPageData Data { get; set; }
protected override void OnLoad(EventArgs e)
{
//your code
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
base.OnLoad(e);
}
}
Instead of a Page_Load() method, override OnLoad() and call base.OnLoad() in PerformanceFactsheet
Uhm, I maybe wrong, but I believe this is due to inheritance: you are overwriting the FactsheetBase Page_Load method in the derived class.
In order to have it executed you should do something like
public partial class PerformanceFactsheet : FactsheetBase
{
protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load( sender, e );
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
EDIT: n8wrl definitely gave you a cleaner solution (I am not a ASPX programmer).
try this one
public partial class PerformanceFactsheet : FactsheetBase
{
public PerformanceFactsheet()
{
this.Load += new EventHandler(this.Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
divPerformance.Controls.Add(this.Data);
}
}
public abstract class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
public FactsheetBase()
{
this.Load += new EventHandler(this.Page_Load);
}
new protected void Page_Load(object sender, EventArgs e)
{
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
try this one:
public partial class PerformanceFactsheet : FactsheetBase
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
// do stuff with the data extracted in FactsheetBase
divPerformance.Controls.Add(this.Data);
}
}
public class FactsheetBase : System.Web.UI.Page
{
public MyPageData Data { get; set; }
protected virtual void Page_Load(object sender, EventArgs e)
{
// get data that's common to all implementors of FactsheetBase
// and store the values in FactsheetBase's properties
this.Data = ExtractPageData(Request.QueryString["data"]);
}
}
Make the page load public, and call it in a manner like this from the other page:
this.myPageOrUserControl.Page_Load(null, EventArgs.Empty);

Categories

Resources