For some reason i am created my own version of System.Web.UI.Page class and inherited the deafult Page class. Now i want to set Title of every page that using my custom Page class on Page_Init event. Can you please suggest me the best way without creating a new version of Page_Init method on drrived classes
public class MyPage : Page{
protected void Page_Init(object sender, EventArgs e){
//do some basic opeation
}
}
public partial class Login : MyPage{
//want to set title from Here
}
Thanks
Ravi Mittal
What's wrong with doing this.Title?
I'm not too sure if you're setting the AutoEventWireup property but I'd suggest you to override the OnInit method rather than using Page_Init
You can set it just via Title property
protected void Page_Init(object sender, EventArgs e){
//do some basic opeation
this.Title = "Some title";
}
If you want to make it different for each sub page, you can make abstract property
public abstract class MyPage : Page
{
public abstract string PageTitle {get;}
protected override void OnInit(EventArgs e){
//do some basic opeation
this.Title = PageTitle;
base.OnInit(e);
}
}
and sample page
public class SubPage : MyPage
{
public override string PageTitle
{
get { return "Some sub title";}
}
}
Related
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 ;).
I have several controls that inherit BaseUserControl. BaseUserControl inherits System.Web.UI.UserControl.
I want to override OnLoad event like this:
public partial class MyControl1 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
This works great, the only problem is that I have to copy this piece of code across 3 controls, which I do not like. (I do not have access to Base class, since it is inherited by 100s of controls.)
So, my result currently looks like this:
public partial class MyControl2 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl3 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
What are the good ways to refactor this? One way is to extract
this.Value = myCustomService.GetBoolValue();
///More code here...
to a separate method, but I was wondering if there's a way that will allow us specify override event only once?
You could create an extra base class for those controls sharing functionality and make this class inherits from BaseUserControl
// Change YourBaseControl by a meaningful name
public partial class YourBaseControl : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl2 : YourBaseControl
{
...
}
public partial class MyControl3 : YourBaseControl
{
...
}
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()
The variables in the partial class _Default get reset in the function call as mentioned in the code below. But, on marking the properties as static, their values are retained.
My questions is, why does this happen? Doesn't each function call use same instance of page class?
public partial class _Default : System.Web.UI.Page
{
public double ValueToConvert { get; set; }
public double ConvertedValue { get; set; }
protected void Page_Load(object sender, EventArgs e){}
protected void btnUC_Click(object sender, EventArgs e)
{
//In this method, the non-static properties ValueToConvert and ConvertedValue
//get reset. But why?
}
}
Doesn't each function call use same instance of page class?
Well each request will create a new instance of the class. Heck, they could be on different processes or even different machines. If you have multiple method calls within the same request that will use the same instance, but otherwise you need to work out how you expect the state to be propagated. You could propagate it via the client (viewstate) or store it somewhere server-side (e.g. in a database).
you can solve this problem by using hiddenfields and wrapping them with the properties like this
public partial class _Default : System.Web.UI.Page
{
public double ValueToConvert \
{
get{
return hfValueToConvert.Value;
}
set{
hfValueToConvert.Value = this.value.ToString();
}
}
public double ConvertedValue
{
get{
return hfConvertedValue.Value;
}
set{
hfConvertedValue.Value = this.value.ToString();
}
}
protected void Page_Load(object sender, EventArgs e){}
protected void btnUC_Click(object sender, EventArgs e)
{
//In this method, the non-static properties ValueToConvert and ConvertedValue
//get reset. But why?
}
}
now add two HiddenField controls name hfValueToConvert & hfConvertedValue to your aspx page.
I am trying to access a function placed in master page code-behind from another ASPX page as follows.
Main.master.cs:
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public static bool test()
{
return true;
}
}
Product.aspx:
<%# Page Language="C#" MasterPageFile="~/Main.master" EnableEventValidation="false"
AutoEventWireup="true" ValidateRequest="false" CodeFile="Product.aspx.cs" Inherits="Common_Product" Title="Product" %>
...
<asp:Label id="test123" runat="server" />
Product.aspx.cs:
using SiteABC.Accelerate;
public partial class Common_Product : SiteABC.Accelerate.SerializePageViewState
{
private void Page_Load(Object sender, EventArgs e)
{
Main cm = (Main)Page.Master;
test123.Text = "yo | " + cm.test();
}
}
This results in a compiler error:
Compiler Error Message: CS0176: Member 'Main.test()' cannot be accessed with an instance reference; qualify it with a type name instead
What is wrong in this scenario?
Thank you.
Try this:
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public bool test()
{
return true;
}
}
Error said it quite clearly, you can't access static methods with an instance reference.
You need to do it like this:
test123.Text = "yo | " + Main.test();
However, I'm not sure if it's the best practice to put methods like this to your MasterPage... You should create a new class and use that instead.
Change your Test so that it's a property
public partial class Main : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
public Property bool test()
{
get { return true; }
}
}
You can not access static method using instance object.
It should be
Main.test();