I have a page that contains a static method like this:
public partial class _Default : System.Web.UI.Page
{
[WebMethod(EnableSession = true)]
public static string Aware()
{
return DateTime.Now.ToString();
}
}
How I can call this method from a Master Page?
_Default.Aware() as for any other static method
EX:
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_Default.Aware();
}
}
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 ;).
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";}
}
}
I am working on an asp .net project and i have a main ascx control which is divided by two RadSpliters. On load of the main.ascx the app is loading two other controls, the control1.ascx and control2.ascx. In the control1 i have a treeview and on selected node of the treeview i want to reload the control2.ascx. Is there a way to do this. Below i am pasting the code that i am using to do that but is not working. Any help or suggestions please?
public partial class Control1: System.Web.UI.UserControl
{
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
}
public interface IReload{
public void reload();
}
public partial class Control2: System.Web.UI.UserControl, IReload
{
}
public partial class Control1: System.Web.UI.UserControl
{
IReload _r;
public IReload setReload
{
set { _r = value; }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
if(_r != null){
_r.reload();
}
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
c.setReload(codeEditor);
}
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();
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);