How to load menu only once time in Master Page? - c#

I created Master page which has got mainNavigator panel on top of page that is a web user control(BuildMenu.ascx). I am filling UC Menu in master page's loading :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="IntermMaster.master.cs" Inherits="MyProject.IntermMaster" EnableViewState="true" %>
<%# Register src="Utils/BuildMenu.ascx" tagname="BuildMenu" tagprefix="uc1" %>
>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<uc1:BuildMenu ID="BuildMenu2" runat="server" />
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
This is loading in postback event:
BuildMenu.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Fill Menu from DataBase (Compare SiteMap...)
}
Every post back BuildManu.ascx is loading every time tihs is really bored me. How can i solve it. I want to do only one time load BuildMenu.ascx (in master page)

Unless you want to use frames (and you probably do not), the control has to be reloaded each time so it can be rendered. The best you can do is to use server-side output caching so that it takes less processing time to load the control.
To do output caching, put this in your page:
<%# OutputCache Duration="[Number of Seconds]" VaryByParam="None" %>

The load method WILL be called every time a postback occures (except for AJAX pages, but let's not go there). Take a look at the ASP.NET page lifecycle.
What you can do is just return from the controls Load event if the value of IsPostBack is true.
However, if the control in question is static (or almost static) in content you could try using output cashing on the server, that way the control will be loaded once in a while, and the rest of the times, the server will just use it's cashed copy.

i have a better idea why dont you sue a session it will help you
make like this ::
protected void Page_Load(object sender, EventArgs e)
{
if session(ispostback") <> "menuloaded"
{
// Fill Menu from DataBase (Compare SiteMap...)
Session("ispostback")="menuloaded"
}
this will work for sure

Related

Master Page is getting called twice in Response.Redirect asp.net c#

When I change the index in masterpage dropdown list. This is what happens:
1. The current page load event is getting called .
2. Master Page contents gets called and it does not hit (!PostBack) functions which is how it is supposed to be .
3. Selected index changed event is called from where it is redirected to destination Page
4. Destination Page content is getting called.
5. Master Page content is getting called as and this time it hits (!PostBack) functions.due to that my selected value gets reset as I take this value from database and insert first time .
I am confused as to why it is happening, I tried many things but didn't get the result. Also I am not sure whether step 1 and step 2 are also correct process.
I am really struggling with this and need some help.
This is my code
SourcePage.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="PlantOverview.aspx.cs" Inherits="PlantDevelopment.PlantOverview" MasterPageFile="~/Master/MasterPage2.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<meta name="viewport" content="width=device-width, initial-scale=1">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="flex-lg-wrap wrapper">
</div>
</asp:Content>
SourcePage.aspx.cs
using System;
namespace PlantDevelopment
{
public partial class PlantOverview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
}
}
DestinationPage.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Master/MasterPage2.master" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="PlantDevelopment.TestPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<!-- Latest compiled JavaScript -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style type="text/css">
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="flex-lg-wrap wrapper">
</div>
</asp:Content>
Destinationpage.aspx.cs
using System;
namespace PlantDevelopment
{
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
}
}
MasterPage.Master
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage2.master.cs" Inherits="Master_MasterPage2" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<nav class="navbar navbar-expand-lg" style="background-color: #081A51; margin-top: 15px;">
<div>
<div class=" justify-content-end">
<div class="btn-group btn-style1">
<asp:DropDownList ID="DD_Line" class="dropDownWindow_master" runat="server" Width="350px" ForeColor="black" Font-Size="22px" AutoPostBack="true" OnSelectedIndexChanged="DD_Line_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="0" Text="Select Line"></asp:ListItem>
<asp:ListItem Value="Line1"></asp:ListItem>
<asp:ListItem Value="Line2"></asp:ListItem>
<asp:ListItem Value="Line3"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</div>
</nav>
<div class="content" id="pageContent">
<asp:ContentPlaceHolder runat="server" ID="ContentPlaceHolder1"></asp:ContentPlaceHolder>
<div class="footer-wrapper" id="footerWrapper">
</div>
</div>
</form>
</body>
</html>
MasterPage.cs
using System;
using System.Web.UI;
public partial class Master_MasterPage2 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//blanks dropdown and insert from DB (this is the issue the previous selected value gets reset)
}
}
protected void DD_Line_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/Pages/TestPage.aspx", false);
}
}
Ok, we might have to step back a bit more.
When using a master page, and you do a simple navigate to "some" page That page of course is a URL based on the "child" page.
So, when above occurs, then we find:
child page load event fires - (postback = false)
master page load event fire (postback = false).
Ok, at this point in time, then any button click (child page).
then
Child page load event tigger, post-back flag = True
master page load event trigger, post-back flag = True
our button click (or event) in child page runs.
(or event stub in master page runs if event from masterpage)
So, in ALL cases, both child page load, master page load will fire.
However, if we navigate to a new page?
Then we are in effect starting over, and the master page is to be considered a full re-load, and post-back will be false on first page load (after navigate) to the new page. (this holds true for both master + child page).
So, master page does not "only" fire one time on the whole application start, and thus only ever have ONE postback event = false, and then additional post-backs (from master or child) will then of course have post-back = true.
And in fact, page load for master page triggers every time for any event on the current page (just like child page load triggers every time also).
Of course, if we in the child (or master) we re-navigate to the same page using code?
Then again we are starting over.
If you have a button (say in child page) that does some stuff, and THEN navigates to the SAME page, then yes, the master page load event will fire 2 times. (so will the child page load event).
So, a trigger in code such as a response.Redirect() to the same page should be avoided, since it will trigger both load events two times. So, the reason being is that in the current page, if we trigger a button click/event, then both page loads will trigger as always, then the child event code stub runs. But, if for some reason that trigger is a navigate to the SAME page?
Then again we starting over, and both load events will trigger again, - and both master/child will show is-postback = false.
So, a "navigate" to the same page will trigger both on-loads, and both will be postback = false. And this holds true EVEN if we are/were on the SAME current page when that navigation occurs.
And for such navigation to occur on the same page means both load events will have triggered, then your code stub run, and then assuming that stub triggers navigation to the same page, then that will of course cause both post-backs to occur again. (with post-back = false, like we are starting over again). So yes, this is expected, and thus we get 2 page loads for master and child.
From what you suggest/state, the solution would be to remove the current page code that is causing a navigation to the same page. (maybe you test/check the current url then???).
So for any event (in child or master page), then both events trigger (PostBacks = true). However, if you navigate to the same page (as a result of running that event)?
Then if that navigation is the result of code behind in the same page? Well, then yes, we will see/have both load events trigger two times, first time is the "button" or code that we run from that page. (which means postback = true). The 2 load events fire, then we navigate, and if we navigate to the same page, then we are in effect starting over. In such a case, 4 load events will have occurred.
As I stated, the solution is to NOT use code to navigate to the same page we are on, and I see "little" reason for this to occur. About the only time I had code navigate to same page in code behind was some code to re-fresh/re-load the current URL to re-set the page).

How to communicate between user controls in Asp.Net

I have 5 files.
Default.aspx
Search.ascx
SearchSQL.ascx
Grid.ascx
GridSQL.ascx
I have registered the ascx files in the default.aspx page and use properties to expose the controls to the default page. And that works great.
My issue is how do I send data back and fourth between the different ascx pages? If I register on any of those it will give me a Circular file reference error.
Using public properties, I have the Search.ascx registered on the GridSQL.ascx to pass the search parameters into Gridsql string, and then the GridSQL.ascx on the Grid.ascx file to pass the sql string to the grid databind.
There has got to be a much easier way to pass data BACK & FOURTH between pages, or am I wrong? When you try to register on the other page to pass data back to the page that sent it, you get the circular file reference error. I have heard a few resolutions like changing file structure, which I have tried, and also about Batch, but that kills performance. Believe i have spent days trying to find resolutions on this. I was going to comment on some questions but Stack does not allow me until I have 50 Rep.
My company is requiring us to use all separate files from now on and I just cant believe this is the best way to communicate between user controls.
Proper way is you want to bubble up the child control's event to parent.
Then let parent to forward the event to other controls.
Note: Here is the demo. You might want to rename delegates and methods which make sense to your scenario.
Search (User Control which fires the event)
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="Search.ascx.cs" Inherits="DemoWebForm.Search" %>
<asp:TextBox runat="server" ID="SearchTextBox" />
<asp:Button runat="server" ID="SearchButton"
Text="Search" OnClick="SearchButton_Click" />
public delegate void MessageHandler(string searchText);
public partial class Search : System.Web.UI.UserControl
{
public event MessageHandler SearchText;
protected void SearchButton_Click(object sender, EventArgs e)
{
SearchText(SearchTextBox.Text);
}
}
GridSql (User Control)
Finally, GridSql.ascx receives the search text.
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="GridSql.ascx.cs" Inherits="DemoWebForm.GridSql" %>
<asp:Label runat="server" ID="SearchTextLabel"/>
public partial class GridSql : System.Web.UI.UserControl
{
public void SearchTextMethod(string searchText)
{
SearchTextLabel.Text = searchText;
}
}
Parent
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="Parent.aspx.cs" Inherits="DemoWebForm.Parent" %>
<%# Register src="~/Search.ascx" tagname="Search" tagprefix="uc1" %>
<%# Register src="~/GridSql.ascx" tagname="GridSql" tagprefix="uc2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:Search ID="Search1" runat="server" />
<uc2:GridSql ID="GridSql1" runat="server" />
</form>
</body>
</html>
public partial class Parent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Search1.SearchText += m => GridSql1.SearchTextMethod(m);
}
}

asp,net using site.master page

I am trying to change the css class attribute that has been present on the site.master page at run time and I cant really get any head way I have so far tired
mainContainer.Attributes.Add("style", "background-image('myImage.png')");
AND
mainContainer.Attributes.Add("class", "className");
BUT non of these let me change the css of the master file at run time. i am using asp.net using c#
this is the code on the master page
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
ContentPlaceHolder is element wich won't be existed in output html code. It only defines a region. You can try to change div with class "main". Just add runat="server" and id attributes and access from the code.
<div id="MainDiv" class="main" runat="server">
and then
MainDiv.Attributes.Add...
You need to partially load the master page in the other child pages like below...
<%# MasterType VirtualPath="~/Site1.Master" %>
Then in the page load of the child page..put
protected void Page_Load(object sender, EventArgs e)
{
HyperLink contact_menu = (HyperLink)Master.FindControl("contactmenu");
contact_menu.CssClass = "current";
}
Change as per your need..
Enjoy..
You first need find the control on the master page
Image img = Page.Master.FindControl( "layoutStyleSheet" ) as Image;
then add style to it
img.Attributes.Add("class", "className");

Problem with setting visibility of controls in child master page in asp.net

I have a parent master page (Master.Master) and child master page (Child.Master). The Child.Master inherits Master.Master master page file. Now in the Child.Master i want to set the visibility of Div (whose ID is Div1) to false, for which i'm using the following code:
protected void Page_Load(object sender, EventArgs e)
{
this.FindControl("Div1").Visible = false;
}
Here is the code in the Child Master Page file:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Webstore.Master.cs" Inherits="WebStore.WebStoreMaster" MasterPageFile="~/Login.Master" %>
<asp:Content ID="UserMaster" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div id="Div1" runat="server">
<div id="Sidebar" runat="server" style="float: left; margin-top: 100px; margin-right: 20px;">
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
The compiler is giving me the following error:
Object reference not set to an instance of an object.
Can someone explain why is it happening so ??
Thanks in anticipation
EDIT:
In this case, if the div is a top level element, and you are in the page_load of the child master page in which the div resides, you should just be able to do
Div1.Visible = false;
Why not use a Panel control?
you should say
this.Master.FindControl("Div1").Visible = false;
Try setting #MasterType in Content Pages and in Child Master Pages. Below are some reference links
http://msdn.microsoft.com/en-us/library/ms228274.aspx
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+%28.NET+Zone%29
http://dotnetslackers.com/community/blogs/haissam/archive/2008/02/11/mastertype-directive-in-content-page.aspx
Thanks
Master page is loaded is after page_load. Therefore when you attempt to address the master page during page_load it's properties and methods are not yet available. Move this down in the page life cycle. ASP.NET Page Life cycle, Another SO answer on Masterpage/page life cycle. the child master is loaded during the page_load, and parent master is loaded during the child master page_load.

From UserControl to parent master page

I have a weird scenario but this was how it was designed before me. Basically I have userControl, and there is a child.masterpage
in the userControl in the ascx file it contains the following
<div><%=_template%></div>
the child.masterpage inherits from a parent.masterpage, in the child.masterpage there is a call to the userControl
<asp:Content><ucc:UserControl></ucc>
the parent.masterpage has other fields in it and it has a .cs file with a c# function
public void passVal(string s)
Now what I want to do is to pass a value from the user control directly to the parent.masterpage function so that I can put it in the parent.masterpage literal I have created.
How can I achieve this (again, this is existing design and I cant turn things around) I am just adding a functionality.
<%# Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/main.master" %>
<%# Register Src="UserControl.ascx" TagName="Ord" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="in"><uc:OrderReceipt ID="myord" runat="server" Visible="true"/>
<div style="margin-bottom:30px;">
Back to Home Page
</div>
</asp:Content>
You can use the Page.Master property to get a hold of the master page instance.
protected someEvent(object sender, EventArgs e)
{
(Page.Master as ChildMaster).passVal("some string");
}
More of an Answer:
I was just reviewing your OP and realized something. The code that kept puzzling me was the user control.
in the userControl in the ascx file it
contains the following
<div><%=_template%></div>
I haven't seen the code behind but my guess is that the user control is simply used to output dynamic HTML. I bet if you looked at the code behind (.cs file) of the user control, you would find a variable called _template. It is a string variable that is pumped with html at run time.
Now, that doesn't answer your question but, if you didn't already know that ... it is good to know =P
Now, the next mystery is the one concerning your missing code behind file for the child master page.
My theory is that whoever made it did it with some error that would cause it not to automatically generate a code behind file. Or, they made it from scratch and just simply added it to the project but neglected to make a code behind as well.
I made a master page, then made another one called child. I am able to subclass it and here is what the markup and code behind look like.
<%# Master Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="Child.master.cs" Inherits="Child" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
</asp:Content>
public partial class Child : Master
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
In comparing the markup to yours, the key difference here is that mine explicitly mentions a CodeFile attribute.
Create a new cs file and following the naming convention. Then add the CodeFile and Inherits attributes to your child master page. This should wire everything up correctly and allow you to start adding methods and such to the child master page.
Let me know where you are at and we'll take it from there. GL

Categories

Resources