how to update master page in all child pages without refreshing - asp.net

As i am new to asp.net i need some help from you,
i am using a MasterPage and child pages.
In master page I have a Label in which it shows the items added to cart.
when i am adding a product in products page.aspx it is updated in master page as (1 item) and showing in the label but when i am coming to homepage.aspx it is not showing i am using same master page in home.aspx.
If i refresh the home.aspx, it is updated. I need to update it without refreshing.

Sample code look like this
MasterPage markup: Here's the label in your masterpage which you want to be update from childpage
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
MasterPage.cs:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string labeltext
{
get
{
return Label1.Text;
}
set
{
Label1.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
ChildPage.cs:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.labeltext = "Your value"; // set your value
}

Related

How to access content page control from master page in asp.net c#?

I have a literal Control in my default.aspx, and i want to access this from my masterpage.When i try to do this,i got null exception error(object reference missing).
You can use FindControl in your master page.
Content page:
<asp:Literal runat="server" ID="myLiteral"></asp:Literal>
And in your master page load (or other place):
protected void Page_Load(object sender, EventArgs e) {
var myLiteral = ContentPlaceHolder1.FindControl("myLiteral");
if (myLiteral != null) {
((Literal)myLiteral).Text = "Hello World!";
}
}
Where ContentPlaceHolder1 is the ID of your placeholder in the master page.

How to refresh a web page in asp.net

In my code i added dynamically some text-boxes and labels.
Then by dropdownlist event i want to refresh the page in a way that all the added text-boxes and labels will remove from aspx
how can it be done?
I assume you're using C#.
You can redirect to the current page by doing this from your code-behind:
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
Add AutoPostBack="True" for the DropDownList
Add an event to DropDownList for SelectedIndexChange
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
In the Code-Behind file :
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
Label1.Visible = false;
}
else
{
Label2.Visible = false;
}
}

Sending data from usercontrol to main page on postback

I want to create a usercontrol for retrieving and filtering a dataset. The problem I'm having is that the controls to be populated are not in the usercontrol, but on the main page on which the usercontrol exists. The reason for this is that the controls that are to be populated are different on each page.
If I could get some help solving the following simplified problem, I will probably be on my way to solve the bigger problem:
<body>
<form id="form1" runat="server">
<div>
<uc1:uc1 ID="uc1" runat="server" />
<asp:Label ID="lbl" runat="server"></asp:Label>
</div>
</form>
</body>
What I want to do is set the label on the main page by clicking a button on the usercontrol. Problem I'm having is that the usercontrol postback happens after the main page.
Thanks!
I managed to solve it by calling Parent.Page. I also implemented an interface so that I can call different pages' methods from the user control. Not sure if it's a good solution, but it will do:
interface IFilter
{
void SetLabel(string text);
}
public partial class uc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
((IFilter)Parent.Page).SetLabel("changed");
}
}
public partial class _Default : System.Web.UI.Page , IFilter
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
public void SetLabel(string text)
{
lbl.Text = text;
}
}
create a custom event in user control
create a property in usercontrol
fire the event when the button is clicked & set the property value
access the property in the main page in user control's eventhandler

ASP.NET how to access public properties?

I have two pages page1.aspx and page2.aspx, both have code behind with partial classes.
How do i access public property message on page1.aspx from page2.aspx ?
public string message { get; set; }
Update
I just read that the one is MasterPage and the other is the client to masterpage ?
then its diferent way.
Page to Page
If you have 2 simple diferent pages.
I have done it this way.
Its a post value, by using asp.net tricks :)
On Page2.aspx add this on top.
<%# PreviousPageType VirtualPath="Page1.aspx" %>
and how I read from Page1.aspx on code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Page.PreviousPage != null)
{
if(Page.PreviousPage.IsCrossPagePostBack == true)
{
txtGetItFromPreviusPage.Text = PreviousPage.SomeString;
}
}
}
}
On Page1.aspx
the button that send me to Page2.aspx
<asp:Button ID="btnEna" runat="server" Text="Send Some variables to other page"
PostBackUrl="Page2.aspx"
onclick="btnMoveSelection_Click" />
and the code that I use for Page1 calculations or other thinks
public string SomeString
{
set
{
ViewState["txtSomeString"] = value;
}
get
{
if (ViewState["txtSomeString"] != null)
return ViewState["txtSomeString"].ToString();
else
return string.Empty;
}
}
protected void btnMoveSelection_Click(object sender, EventArgs e)
{
// some final calculations
}
If the one is the Master page, and the other is the page that use the master.
The Master Page
<body>
<form id="form1" runat="server">
<div>
<asp:Literal runat="server" ID="txtOnMaster"></asp:Literal>
<br />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
and the code behind
public partial class Dokimes_StackOverFlow_MasterPage : System.Web.UI.MasterPage
{
public string TextToMaster
{
get { return txtOnMaster.Text; }
set { txtOnMaster.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
// here I find the control in the client page
Control FindMe = ContentPlaceHolder1.FindControl("txtOut");
// and if exist I set the text to client from the master
if (FindMe != null)
{
((Literal)FindMe).Text = "Get from Master Page";
}
}
}
and now the Page1.aspx that have the previus master page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Literal runat="server" ID="txtOut"></asp:Literal>
</asp:Content>
and the code
protected void Page_Load(object sender, EventArgs e)
{
// here I set the text on master page from client
((Dokimes_StackOverFlow_MasterPage)Master).TextToMaster = "Set from Client";
}
If you are NOT in a sessionless environment, then in the transmitter page, push your string (or your object - e.g., a Dictionary ) into session:
Session("MyVar") = "WhatEver"
In the receiver page, you can get it back with:
MyPreviousVar = Session("MyVar")
If you want a message property on every page. You could implement your own BasePage and define the message property in your base page. Then derive subsequent pages from your custom base page. That way all of your pages will always have a message property.
However, this isn't going to keep the message property constant through out each page. If you are trying to pass values between pages then you should use either session state or a querystring
This MSDN page may be of use to you.
You shouldn't really be doing this, pages should be standalone entities. If you need to pass this data from one form to another, consider using the querystring, or posting your form to the second page.
OK. Have you tried then Page.Master.Property?

Pass MasterPage ImageButton event to content Page

I have ImageButton in a MasterPage. I want the OnClick event to fire and be captured by the .ASPX page hosted inside the MasterPage?
MasterPage:
<asp:ImageButton ID="btnClear" OnClick="Clear_Click"
ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear"
width="38" height="39"/>
The masterpage is actually a child of the page (in fact, it's a UserControl). We don't want the page to have to be aware of the intimate details of its child controls (thats why we delegate those aspects to those controls in the first place), so the correct approach would be to handle the click event on the master page and from there fire another event on the masterpage which the page handles:
Master:
public event EventHandler SomethingHappened;
protected void Button_Click(object sender, EventArgs e)
{
OnSomethingHappened(EventArgs.Empty);
}
protected void OnSomethingHappened(EventArgs e)
{
if(this.SomethingHappened != null)
{
this.SomethingHappened(this, e);
}
}
Page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//allows us to change master pages
if(this.Master is MyMaster)
{
((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
}
}
private void HandleSomethingHappened(object sender, EventArgs e)
{
//deal with it
}
I would recommend specifying a strongly typed master page in your content page and exposing the event on the master page side
Here's a good MSDN reference
This is similar to what Rex M specified but just simplifies accessing the Master Page a little bit.
// Master Page Code
public event EventHandler ClearClick
{
add
{
this.btnClear.Click += value;
}
remove
{
this.btnClear.Click -= value;
}
}
// Content Page markup
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
// Content Page Code
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Master.ClearClick += new EventHandler(OnClearClick);
}
private void OnClearClick(object sender, EventArgs e)
{
// Handle click here
}

Resources