Problem with conditional statements in asp.net - c#

I'm trying to do the following in a aspx page:
<%# Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
<% if (!isOld) %>
<% { %>
<p>display this</p>
<% } %>
</asp:Content>
isOld is a public bool variable from the cs file mentioned in the namespace.
But unfortunately it gave me an unknown error.
I could do something similar in JSPs, but after googling around for a while, I'm not so sure whether the above is achievable in ASP.NET? (Am I missing a tag declaration, or do I have to write the entire tag lib myself?)
Thanks.
EDIT: I just got an unknown error. I have a feeling that the code above has either the wrong syntax or are totally off the wrong track. I tried the following code, and there was no error, but the bool variable is always false:
<% #if !isOld %>
<p> display this</p>
<% #endif %>

in your code front:
<%# Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
<asp:PlaceHolder runat="server" id="PlaceHolderIsOld">
<p>display this</p>
</asp:PlaceHolder>
</asp:Content>
and then in your code behind:
protected void Page_Load(object sender, EventArgs e){
PlaceHolderIsOld.Visible = IsOld;
}

Related

How can I use a variable which is defined in .cs file in .aspx file?

Default.aspx want to use a variable daysBefore which is defined in PublicPar.cs , how can I do ?
BTW, at present Default.aspx is not correct.
Default.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Links.aspx.cs" Inherits="LinkTabs.Links" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="forNote">
<span style="color:red;">Note:</span> Days: <%= daysBefore.toString() %>
</div>
</asp:Content>
PublicPar.cs
namespace LinkTabs.App_MyCode.BLL
{
public class PublicPar
{
public const int daysBefore = 30;
}
}
that should work fine. But, intel-sense should have perhaps saved you!
So, try this:
<div id="forNote">
<span style="color: red;">Note:</span> Days: <%= daysBefore.ToString() %>
</div>
Note the "casing" in the .ToString().
However, while typing, intel-sense should suggest and offer the correct syntax here.

ASP.Net Nested Master Pages - How Deep Can You Nest?

This is long, so please bear with me. I just want to lay out the situation as completely as possible...
Regarding master pages in ASP.Net - I know that you can nest master pages and that you can have multiple nested master pages at the same level (the top-level master as the "parent" and multiple "children", which are the first-level-nested master pages). Can you go more than one level deep in nesting master pages (to a "grandchild" nested-master) and still be able to reach up the chain to the top-level master page using the "Master.Master.Master" syntax?
The situation:
I'm working on a C# ASP.Net application. I have a three master pages: Site.master (the top-level master page for the site), SiteWizard.master (nested from Site.master), and WizardTransactionDriver.master (a master page nested from SiteWizard.master).
Relevant markup is as follows:
Site.master:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="RegE_BranchV3.SiteMaster" %>
<body>
<form runat="server">
<div class="container body-content">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="WizardContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
SiteMaster.cs
namespace SomeNameSpace
{
public partial class SiteMaster : MasterPage
{
#region ===== Properties ====
public TLoginSession LoginSession { get; set; }
}
}
SiteWizard.master - nested under Site.Master
<%# Master Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SiteWizard.master.cs" Inherits="RegE_BranchV3.SiteWizard" %>
<%# MasterType VirtualPath="~/Site.Master" %>
<asp:Content ID="WizardAnswers" ContentPlaceHolderID="MainContent" runat="server">
<asp:ContentPlaceHolder ID="WizardQuestions" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="TransactionEntry" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
AffidavitWizardText.aspx - content page linked to SiteWizard.master
<%# Page Title="" Language="C#" MasterPageFile="~/SiteWizard.Master" AutoEventWireup="true" CodeBehind="AffidavitWizardText.aspx.cs" Inherits="RegE_BranchV3.WebPages.AffidavitWizardText" %>
<%# MasterType VirtualPath="~/SiteWizard.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="WizardQuestions" runat="server">
</asp:Content>
And code-behind in AffidavitWizardText.aspx.cs:
namespace somenamespace
{
public partial class AffidavitWizardText : System.Web.UI.Page
{
private TBranchSession fBranchSession;
private IBranchCase2 fBranchCase;
private TLoginSession fLoginSession;
protected void Page_Load(object sender, EventArgs e)
{
Master.Master.LoginSession = TApplLib.LoadLoginSession(Session);
}
}
}
The Master.Master.LoginSession reference works just fine.
WizardTransactionDriver.master:
<%# Master Language="C#" MasterPageFile="~/SiteWizard.Master" AutoEventWireup="true" CodeBehind="WizardTransactionDriver.master.cs" Inherits="RegE_BranchV3.WebPages.WizardTransactionDriver" %>
<%# MasterType VirtualPath="~/SiteWizard.Master" %> <%--SiteWizard--%>
<asp:Content ID="Content1" ContentPlaceHolderID="WizardQuestions" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="TransactionEntry" runat="server">
<asp:ContentPlaceHolder ID="TransactionItem" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
I have a content page WizardTransactionText.aspx - its master page is WizardTransactionDriver.master
<%# Page Title="" Language="C#" MasterPageFile="~/WebPages/WizardTransactionDriver.master" AutoEventWireup="true" CodeBehind="WizardTransactionText.aspx.cs" Inherits="RegE_BranchV3.WebPages.WizardTransactionText" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TransactionItem" runat="server">
</asp:Content>
WizardTransactionText.aspx.cs - the content-page code-behind:
namespace somenamespace
{
public partial class WizardTransactionText : System.Web.UI.Page
{
private TBranchSession fBranchSession;
private IBranchCase2 fBranchCase;
private TLoginSession fLoginSession;
protected void Page_Load(object sender, EventArgs e)
{
Master.Master.Master.LoginSession = TApplLib.LoadLoginSession(Session);
}
}
}
Visual Studio does not recognize the Master.Master.Master.LoginSession reference above and gives me an error. It recognizes the Master.Master.LoginSession reference in the higher level content page AffidavitWizardText that is bound to SiteWizard.master at the first level of master-page-nesting. Why doesn't it recognize a nested-nested reference? Is there a limit to how many levels (how deep) you can nest master pages?
In this thread, bkaid says "you can nest as many master pages as you like". That can be taken a couple of ways. The example in the article he links to only shows one level of nesting of master pages. Every article and example I've seen only shows one level of master page nesting. Is there something behind the scenes that limits the number of levels that the code recognizes, even though I can create multiple levels of nested master pages when adding items in the Solution Explorer? We don't have a great deal of ASP.net experience here - is there another issue we're missing, or another path to doing this?
Thanks for any help and insight!

PreviousPage is null when attempting a cross-page postback using a button and the PostBackURL attribute

I followed the tutorial: Getting Public Property Values from the Source Page on MSDN and I started a fresh Web Forms site and created the below two pages. They work just fine. Now, if I copy paste these into my other Web Forms project then PreviousPage == null. I have no idea what the issue could be. There is no errors whatsoever. I just get
messageSTr.Text = "Not a cross-page post.";
UPDATE: I deleted the MasterPage reference in the page declaration and im still getting the error. I copied this projects web.config to the other working one and it still works. Its not my web config, I am at a complete loss here. This is a vital tool that I need for my application.
This page submits to page 1
<%# Page
Title=""
Language="C#"
MasterPageFile="~/Site.Master"
AutoEventWireup="true"
CodeBehind="WebForm2.aspx.cs"
Inherits="WebApplication5.WebForm2" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp:Button id="button1" Text="Submit to PostBackUrl" PostBackUrl="~/WebForm1.aspx" runat="server"/>
This page receives the submit
<%# Page
Title=""
Language="C#"
MasterPageFile="~/Site.Master"
AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication5.WebForm1" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="messageSTr" runat="server"></asp:Label>
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
if (PreviousPage.IsCrossPagePostBack == true)
{
messageSTr.Text = PreviousPage.imaliveStr;
}
}
else
{
messageSTr.Text = "Not a cross-page post.";
}
}
The problem was the FriendlyUrls nuget package was removing the .aspx after my page names so my target page was not WebForm2.aspx but just WebForm2. This made the previous page null.

Setting multilanguage Page title from aspx

I had asked a couple of question about multilanguage in asp.net and I am very grateful because the answers have been of much help.
I now face another problem.
I have the page directive:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Galeria.aspx.cs" Inherits="TerapiaFisica.Galeria" %>
What I want is to make the Title multilanguage.
I know i can do that from code behind with something like this:
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = (string)GetLocalResourceObject("PageTitle");
}
But that's exactly what i don't want to do. I want to make that title multilanguage from the tag in the page directive of the aspx.
Anyone can tell me what to do?
I tried this two options but none of them works:
<%# Page Title=" <%= GetGlobalResourceObject("Global", "PageTitle") %>"
and
<%# Page Title="<asp:Localize Text="<%$ Resources: Global, PageTitle %>"
Will this work for you?
<head>
<title><%= GetGlobalResourceObject("Global", "PageTitle") %></title>
</head>
I don't have my IDE in front of me, but the one you wrote (below) looks wrong
<%# Page Title=" <%= GetGlobalResourceObject("Global", "PageTitle") %>"
Did you try
<title>
<%= GetGlobalResourceObject("Global", "PageTitle") %>
<title>

ASP.NET content incode id

I recently started using Masterpages, the thing is I would like to add text in code to an asp:Content tag.
So my content page markup code is:
<%# Page Language="C#" MasterPageFile="~/Template.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP_Test_WebApp.Default" %>
<asp:Content id="TEST" ContentPlaceHolderID="Main" Runat="Server" />
So now I would like to add Contents to the "TEST" id incode.
But my in code doesn't recognize TEST. If I don't use a masterpage and I give an id to a tag my in code reconigzes it, but now that I started using masterpages it doesn't.
What am I doing wrong?
Content tags don't have any UI on their own, you need to add controls inside them that you can then address in your code e.g.
<%# Page Language="C#" MasterPageFile="~/Template.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP_Test_WebApp.Default" %>
<asp:Content id="TEST" ContentPlaceHolderID="Main" Runat="Server" >
<asp:label runat="server" id="MyLabel"/>
</asp:content>
public partial class Default: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyLabel.Text = "StackOverflow rocks!"
}
}
You don't need that ID. Try to add your content like: this.Controls.Add(mycontentcontrol)

Categories

Resources