render conditional asp:content - c#

I need to render a conditional asp:content
how can I do this please?
**if( culture=fr)**
<asp:Content ContentPlaceHolderId="altColumnContent" ID="altColContent" runat="server">
<div class="altBloc">
content 1
</div>
<!-- END: Alternative Bloc -->
</asp:Content>
**if(culture=en)**
<asp:Content ContentPlaceHolderId="altColumnContent" ID="altColContent" runat="server">
<div class="altBloc">
content 2
</div>
</asp:Content>
When, I put 2 asp:content with the same ContentPlaceHolderId i have erros.
regards

If you just want to change the content based on the culture property value, then there is no need to render different content tags, and you better move the if statement inside your and render different content inside based on the culture value?
this is a pseudo-block for demonstration
<asp:Content ContentPlaceHolderId="altColumnContent" ID="altColContent" runat="server">
<div class="altBloc">
<% if(CultureValue == FIRST_VALUE){ %>
content 1 ==> // YOUR_FIRST_CONTENT
<%} else { %>
content 2 ==> // YOUR_SECOND_CONTENT
<%} %>
</div>
</asp:Content>

Related

How to solve problem in master page in C#?

I have html code and want to split it between site.master and default.aspx
When I put a full code on site.master it works, but when I took a part of code and put it in default.aspx, it doesn't work!
I just want to know how to make default.aspx reach all classes in .css files
This is my tag which cant take a class from .css
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationClinicASP._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<section class="hero-wrap js-fullheight" style="background-image: url('images/bg_3.jpg');" data-section="home" data-stellar-background-ratio="0.5">
<div class="overlay"></div>
<div class="container">
<div class="row no-gutters slider-text js-fullheight align-items-center justify-content-start" data-scrollax-parent="true">
<div class="col-md-6 pt-5 ftco-animate">
<div class="mt-5">
<span class="subheading">Welcome to Mediplus</span>
<h1 class="mb-4">We are here <br>for your Care</h1>
<p class="mb-4">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove.</p>
<p>Make an appointment</p>
</div>
</div>
</div>
</div>
</section>
</asp:Content>

ASPX File only Shows Code

I've been trying to upload my site to my Hostinger domain for a few days now, however for some reason the .aspx file shows the raw code and doesn't show any of the GUI present when run through the browser.
How would you advise?
<%# Page Title="" Language="C#" MasterPageFile="~/Mater Pages/Primary.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
width: 873px;
height: 455px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h2 style="text-align: center">
Hello and welcome to my personal website. I hope that you enjoy your visit.</h2>
<h3 style="text-align: center">
If for any reason you can't find exactly what you are looking for then please use the contact form here and you'll get a response within 2-3 working days</h3>
<h3 style="text-align: center">
<img alt="" class="auto-style1" src="Images/Default/image%20for%20hompage.png" /></h3>
<p style="text-align: center">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Website Ad Test -->
<ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-8284273019831171"
data-ad-slot="7241345648"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</p>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Looking at Hostinger's website it looks to me that they do not support .net framework.
it seems that your server does not execute the code in your .aspx web page, it just returns the content (the source code) of it.
Contact your web server provider and make sure they are serving .NET pages.

Implement two forms in an ASP.net page with one master page, or display the same form at two places

How to implement two forms one in header and other in body of an ASP.net page with one master page, or how can I display the same form at both the places with only one form?
As to your question:
How to implement two forms one in header and other in body of an ASP.net page with one master page, or how can I display the same form at both the places with only one form?
If you need to choose: Concentrate on the second part.
The general method in Web Forms is to have only one <form> in your page, which is .NET's default <form> that surrounds all the content of your .aspx/.master page.
As the rules of HTML state: You cannot have two nested forms in an HTML page.
It means that if you want to have multiple <form> tags in your page, you will have to use it outside of .NET's default <form>.
Basically it means that all of the forms outside of .NET's one will not be part of the View State and you will not be able to use ASP.NET web controls.
However, if you are still considering the first method, you can read some more about it here:
Can we use multiple forms in a web page?
And you can see a really good example implementing it here:
Using multiple forms on an ASP.NET web forms page
Display the same form at two places
Basically, it is an essential part of Web Forms and is used many times.
You can create as many forms as you like by associating as many <asp:Button> elements as you like to different Click events.
To make two forms in the master, one in the header and one in the body:
Put the form contents in the two sections and use two different submit button handlers in the code behind (See example below)
Put in your MasterPage multiple ContentPlaceHolder elements. Use one for each place you would like to load content from your .aspx file.
In your .aspx refer to the ContentPlaceHolder elements with the corresponding ContentPlaceHolderIDs
In this example you can see one form in the Header section and another in the Body section like you wanted:
MasterPageTwoSections.master
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageTwoSections.master.cs" Inherits="MasterPageTwoSections" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<style>
header {
background-color:red;
}
.body {
background-color:green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<header>
<asp:ContentPlaceHolder id="HeaderPlaceHolder" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
<h5>
This is form #1 from the master
</h5>
<asp:TextBox runat="server" ID="txtFirstForm"></asp:TextBox>
<asp:Button runat="server" ID="btnFirstFormSubmit" OnClick="btnFirstFormSubmit_Click"
Text="Submit first form" />
</header>
<section class="body">
<asp:ContentPlaceHolder id="BodyPlaceHolderBeforeForm" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
<h5>
This is form #2 from the master
</h5>
<asp:TextBox runat="server" ID="txtSecondForm"></asp:TextBox>
<asp:Button runat="server" ID="btnSecondFormSubmit" OnClick="btnSecondFormSubmit_Click"
Text="Submit first form" />
<asp:ContentPlaceHolder id="BodyPlaceHolderAfterForm" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
</section>
</div>
</form>
</body>
</html>
MaterPageTwoSections.master.cs
Notice the two submit handlers:
using System;
public partial class MasterPageTwoSections : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFirstFormSubmit_Click(object sender, EventArgs e)
{
}
protected void btnSecondFormSubmit_Click(object sender, EventArgs e)
{
}
}
FirstPage.aspx
Notice Content2 refers to HeaderPlaceHolder in the MasterPage.Content3 and Content4 refer to BodyPlaceHolderBeforeForm and BodyPlaceHolderAfterForm
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPageTwoSections.master"
AutoEventWireup="true" CodeFile="FirstPage.aspx.cs" Inherits="FirstPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderPlaceHolder" Runat="Server">
<p>
This header content is from the FirstPage.aspx
</p>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyPlaceHolderBeforeForm" Runat="Server">
<p>
This body content is from the FirstPage.aspx
</p>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="BodyPlaceHolderAfterForm" Runat="Server">
<p>
This body content is from the FirstPage.aspx
</p>
</asp:Content>

Moving javascript from user control into appropriate master page

I have a scenario like this (simplified for this question):
I have a master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Ads.master.cs" Inherits="ActiveDigitalSignage.Ads" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div class="wrapper">
<form id="form1" runat="server">
<header>
</header>
<section>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</section>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<asp:ContentPlaceHolder ID="script" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
I have a default.aspx page:
<%# Page Title="" Language="C#" MasterPageFile="~/Ads.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div><p>Test</p></div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="script" runat="server">
</asp:Content>
The way its setup is, any header stuff can go in the "head" section. The main stuff goes in the "ContentPlaceHolder" and any javascript goes into the "script" place holder. Everything is working fine as is right now.
The issue I'm running into is I want to use a User Control (.ascx) in my Default.aspx page. However there is javascript in the user control. Because the jQuery is at the bottom of the page (in the "script" section), the javascript in the user user control is breaking. Is there a way to make the javascript in the user control render inside the "script" placeholder?
I know I can just copy and paste the code into a .js file and load the javascript in my master page. The problem with doing that is there are a bunch of ASP.NET tags (e.g. <%= %>) in the javascript that reference controls in the user controls, so copying and pasting this into a .js file would break this.
I'm thinking, off the top of my head, there might be a way in the global.asax page, during prerender, that can detect javascript in a user control and dynamically move it into the proper section?

How do I keep the master page from flickering?

I am building an ASP.NET application and would like to use a master page to display branding and navigation info and move through several content pages. However, every time I try to load new content the entire page flickers. Is there anyway to avoid this?
Master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Interface.Site" %>
<%# Register TagPrefix="customControl" TagName="NavigationBar" Src="NavigationControl/NavigationControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Registration</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="RegistrationMasterForm" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<%-- Contents of this div span the top of the page for branding --%>
<div id="BrandingBanner">
<asp:Image ID="Banner" runat="server"
ImageUrl="Images/BrandingBanner.png"/>
</div>
<%-- Navigation Bar, the contents of this div should remain unchanged --%>
<div id="NavigationBar">
<customControl:NavigationBar ID="navBar" runat="server"/>
</div>
<%-- Contains any forms or controls that may be uniquie to the page --%>
<div id="FormControls">
<div id="FormContent">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</div>
</form>
</body>
</html>
Content:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Interface.WebForm2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Check me out! I am content for the master page!
<asp:Button ID="Button1" runat="server" Text="Next" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
content code behind:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm3.aspx");
}
}
The control event triggers a post back to your page, which typically creates the "blanking" effect that you see. To mitigate this, you may want to consider using some sort of Ajax solution to cause a partial postback or async postback to prevent the "blanking".
However in the particular case of your button click, if all you are trying to do is bring the use to another page, you really should just use an <a href="WebForm3.aspx"> tag and avoid using Response.Redirect.
You are redirecting the page in your codebehind.
This causes the browser to change pages, and redraw the whole thing, at least IIRC. I haven't used updatepanels in ages.
Isnt this only possible if you're using frames? As far as I'm aware the master page and content pages bind together on the serverside and push down the the client as a whole.
Where what you want sounds like using an page for branding and navigation with an inline frame to serve up your convent. This would prevent the outer page from "flickering" on the client side.
Edit: Though it is not the new way to do things. You'd want to look at something using maybe an UpdatePannel or other AJAX style design.
You don't need to move the updatepanel, your redirect is causing the browser to load a new page.

Categories