Calling Content Page Method from MasterPage Method [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
content page class method calling from master page class
I need to access Content Page Method from Master page Event. How can I do this?
Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
Page_Load(object sender, EventArgs e)
{
}
public void MenuClick(string ClkMenu)
{
// Some Code
}
}
MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
//How Can I call MenuClick method from Content Page from Here ???
}
}

This answer is taken from Interacting with the Content Page from the Master Page
You can do this using Delegates.
For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.
Master Page:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (contentCallEvent != null)
contentCallEvent(this, EventArgs.Empty);
}
public event EventHandler contentCallEvent;
}
Content Page:
public partial class Content_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void Master_ButtonClick(object sender, EventArgs e)
{
// This Method will be Called.
}
protected void Page_PreInit(object sender, EventArgs e)
{
// Create an event handler for the master page's contentCallEvent event
Master.contentCallEvent += new EventHandler(Master_ButtonClick);
}
}
And Also add the Below Line Specifying you MasterPage Path in VirtualPath
<%# MasterType VirtualPath="~/MasterPage.master" %>
// This is Strongly Typed Reference

Related

How can I call a Method of a Content from MasterPage in ASP.NET

I want to call a method of a Content from a master page sending a Parameter to manipulate one label.
public partial class MasterCategoria : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
produtosCategoria x = new produtosCategoria();
x.changeLabel("Salada");
}
}
Manipulating this button on this WebForm which is a Content
public partial class produtosCategoria : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changeLabel(string name)
{
lblTexto.Text = name;
}
But this isn't working. How can I do this work?
Thank you guys, and sorry about my english.
The object of type produtosCategoria is already created and it can be accessed from your master page via this.Page.
So to change the label of your content page you can do as in the snippet below.
Also, I added a simple type check so you won't get an error if another content page is loaded
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
// Check if it is the correct content page
if (this.Page.GetType() == typeof(produtosCategoria))
{
produtosCategoria x = (produtosCategoria)this.Page;
x.changeLabel("Salada");
}
}
Note: When code executes in the master page this is the master page and this.Page is the content page

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 ;).

Is it possible to call a function after Page_Load event?

I have this page say test.aspx.Its codebehind is like the code below.
protected void Page_Load(object sender, EventArgs e)
{
}
public void display()
{
// some block statements
}
Since the function display() is outside the Page_Load it is never called. So how do
I make this call to this function after Page_Load.
NOTE: I need this function outside the Page_Load.
Use Page.LoadComplete:
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
protected void Page_Load(object sender, EventArgs e)
{
//call your function
display();
}
protected void Page_PreRender(object sender, EventArgs e)
{
//call your function even later in the page life cycle
display();
}
public void display()
{
// some block statements
}
Here is the documentation that discusses the various Page Life Cycle methods: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx
After Page_Load you have:
PreRender
Render
Unload
If you want to load that function on the time of load only then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
display();
}
}
public void display()
{
// some block statements
}
as this will load only once. But if u want to load it on each post back then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{}
dispplay();
}
public void display()
{
// some block statements
}
There's a default event called Page_LoadComplete which will execute when page is loaded fully
But,If you write your code in a Page_Load Event that code will execute and your controls will be accessible there
So best to call like in a page_load for once with first postback ;)
But, still if you want to go after page load then go for the Page_LoadComplete
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
public void display()
{
// some block statements
}

Preserve value of textbox using View State in Asp.net?

I've a button and a textbox. I want a value to be entered in textbox and when I click on button the page will reload but the value should still be in the textbox. How can I do that. The following code doesn't work
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = ViewState["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}
Response.Redirect does what it says - redirects the request to a NEW page. ViewState won't get applied, ever. If you need a redirection, consider using session instead.
If you don't need a redirection, simply don't redirect and update only parts of the page that need to be updated.
Viewstate can retain the value only till when you are on the same page. You are redirecting to other page. So instead of using viewstate use session.
asp.net webform have already maintained the viewstate on page refresh . don't need any code for handle this operation .
See this : http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_aspnetviewstate
referred from : http://www.w3schools.com/aspnet/aspnet_viewstate.asp
and see this discussion
try this
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}
Since you are redirecting to a new VIEW so VIEWSTATE will not be of any HELP. SO,Use Session
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}

Using the OnInit event in asp.net

I have contentpage overriding the OnInit event of a masterpage. The override works fine, until I put a custom user control on the page: in this case the OnInit event does not fire for the contentpage (no overrides are used in the user control)
What are the possible causes/solutions for this? (I use the OnInit event to create dynamic controls)
Edit:
now i tried this in the content page:
(The OnPreInit part runs, but Masters_Init does not get called...)
protected override void OnPreInit(EventArgs e)
{
base.Master.Init += new EventHandler(Masters_Init);
}
void Masters_Init(object sender, EventArgs e)
{
//code
}
Are you calling the base.OnInit?
public override void OnInit(EventArgs e)
{
// code before base oninit
base.OnInit(e);
// code after base oninit
}
Update
public class Page1 : Page
{
public Page1 : base() {
PreInit += Page_PreInit;
}
void Page_PreInit(object sender, EventArgs e)
{
Master.Init += Master_Init;
}
void Master_Init(object sender, EventArgs e)
{
//code
}
}
Also as mentioned in the comments I would recommend not overriding the events if you don't have to, but if you must be sure to call the base. so in your edit above it should be
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
base.Master.Init += new EventHandler(Masters_Init);
}

Categories

Resources