error when change master page with code in asp.net - asp.net

i have 2 master page
"Main.Master" and "MainAr.Master"
and i have page "Default.aspx" inherit from baseclass
public class BaseClass:Page
{
protected override void InitializeCulture()
{
if (Session["Lang"] != null)
{
if (Session["lang"].ToString() == "ar")
{
this.UICulture ="ar";
this.Culture = "ar-EG";
this.Theme = "ar";
}
else if(Session["Lang"].ToString()=="en")
{
this.UICulture = "en";
this.Culture = "en";
this.Theme = "en";
}
}
else
{
this.UICulture = "en";
this.Culture = "en";
// this.Theme = "en";
}
}
protected override void OnPreInit(EventArgs e)
{
if (Session["Lang"] != null)
{
if (Session["lang"].ToString() == "ar")
{
this.Page.Master.MasterPageFile = "~/MainAr.Master";
}
}
}
}
in onPreinit void master page of default page must be changed in runtime this is the code of inhertnce
public partial class Default : Utilities.BaseClass
but in change master page code i got this error
Content controls have to be top-level controls
in a content page or a nested master page that references a master page.
2 master page code is
<%# Master Language="C#" AutoEventWireup="true"
CodeBehind="Main.master.cs" Inherits="MotahedaWeb.Main" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>
and the default.aspx page code is
<%# Page Title="" Language="C#" MasterPageFile="~/Main.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MotahedaWeb.Default" %>
<%# MasterType VirtualPath="~/Main.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
any ideas? ,thanks

It seems to be correct. You only need to make sure that the ContentPlaceholderIds are consistent across the pages you are swapping between (that's what causing that error message)

Related

How to pass parameters of the controller to the .aspx view?

i have a view in .aspx
the SIGAMVC.Models.Detalle
is generated by the framework, I should never touch it
a solution that I found and it does not work here
VIEW
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SIGAMVC.Models.Detalle>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h4><b><%:Session["TEST"] %></b></h4> <%-- i tried por session but not found --%>
<h1><%=MyValue%></h1> <%-- too --%>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MenuContent" runat="server">
</asp:Content>
CONTROLLER
protected string MyValue { get; set; }
[HttpPost]
public ActionResult Create(Detalle detalle, int id)
{
Session["TEST"] = "RODRIGO ALEX"; //first tried
this.MyValue = "Some Value"; // second tried
return View(detalle);
}
Take a look at using ViewData and ViewBag.
ViewBag.detalle = detalle;
Alternatively:
ViewData["detalle"] = detalle;
For more information on when to use which one this blog post is a great reference.

Find property of sub child usercontrol in aspx page

There is a User Control - ChildUC.ascx with Property "FirstName"
ChildUC.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ChildUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ChildUC" %>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>
<asp:Button runat="server" ID="btnClick" Text="Click" OnClick="btnClick_Click"/>
ChildUC.ascx.cs -
This got a property FirstName
public partial class ChildUC : System.Web.UI.UserControl
{
public string FirstName { get; set; }
}
This ChildUC.ascx user control used in another user control ParentUC.ascx-
ParentUC.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ParentUC.ascx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentUC" %>
<%# Register src="ChildUC.ascx" tagPrefix="uc1" tagName="ChildUC" %>
<uc1:ChildUC runat="server" ID="ucChildUC"/>
The ParentUC.ascx used in a ParentPage.aspx
ParentPage.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ParentPage.aspx.cs" Inherits="SampleDevExpress14._2.UserControls.ParentPage" %>
<%#Register src="ParentUC.ascx" tagPrefix="uc1" tagName="ParentUC" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div>
<uc1:ParentUC runat="server" ID="ucParentUC"/>
</div>
</form>
</body>
</html>
ParentPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
UserControl ucChild = (UserControl)ucParentUC.FindControl("ucChildUC");
TextBox txtNameOfChildUC = (TextBox)ucChild.FindControl("txtName");
txtNameOfChildUC.Text = "From Parent Page";
}
I am able to find the textbox control of ChildUC.ascx in ParentPage.aspx which i showed above in Page_Load event
But how can I find the FirstName Property of ChildUC.ascx in ParentPage.aspx and set a value to it.
Thank you.
Trying using dynamic,
dynamic u = (UserControl)ParentControl.FindControl("childcontrol");
u.firstname = "fawad";
Response.Write(u.firstname);

automatically generating a list of links

Ok so I am trying to make a list of links in a page that is generated using a foreach and loop as long as there are objects in the list. Here is the code that I use to generate the links:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["mamlist"] != null)
{
mamlist = (List<mammifere>)Session["mamlist"];
int i = 0;
foreach (mammifere l in mamlist)
{
mamol.InnerHtml += ("<li><a onClick='select("+i+");' >" + l.Nom + "</a></li>");
i++;
}
}
}
}
For some reason, the links are unclickable. I get this:
How can I make links that do not lead to another page but instead launch a method in the C# code of the page?
You can create LinkButton controls that call subroutines/methods in your ASPX code:
Sample code:
<%# Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>LinkButton Example</title>
<script language="C#" runat="server">
void LinkButton_Click(Object sender, EventArgs e)
{
Label1.Text="You clicked the link button";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
In your specific case, add a ContentPlaceHolder in your master page:
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
In the page where you want the links to appear you add a Content control like so:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Then foreach link you want, do this:
foreach (mammifere l in mamlist)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = l.Nom;
linkButton.OnClick= "LinkButton_Click";
linkButton.ID = l.Nom;
Content1.Controls.Add(linkButton);
}

How to write a meta tag in a .aspx-page?

I have the following code:
<%# page title="בחירת מועמדי הליכוד" language="C#"
masterpagefile="~/Site.master" autoeventwireup="true" %> <asp:Content
ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content> <asp:Content ID="BodyContent" runat="server"
ContentPlaceHolderID="MainContent"> <center> <div class="endMsg"> תודה
רבה על השתתפותך ! </div> </center> </asp:Content>
I want to add the meta-tag:
<meta http-equiv="Refresh" content="60; url=http://your.new/url/here" />
How do I write code which is interpreted as that meta-tag?
Edit:
Based on a comment, I wrote this code:
public partial class Thanks : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int _refresh_In_Seconds = 5;
HtmlMeta metaKey = new HtmlMeta();
metaKey.Name = "Refresh";
metaKey.Content = _refresh_In_Seconds + "; url=Default.aspx";
Page.Header.Controls.Add(metaKey);
}
}
The redirect doesn't work. Can anyone explain why?
Edit 2:
This is another solution to the problem:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<meta http-equiv="Refresh" content="10; URL=Default.aspx">
</asp:Content>
try this
HtmlMeta metaKey = new HtmlMeta();
meta.Name = "Refresh";
meta.Content = _refresh_In_Seconds + "; url=whatEver.aspx";
Page.Header.Controls.Add(metaKey);
You have to use HttpEquiv property of HtmlMeta, so instead of:
metaKey.Name = "Refresh";
put:
metaKey.HttpEquiv = "Refresh";

locate label on content page

I am trying to locate a label on a content page from within User Control(ascx)
Page p = this.Page;
//this line causes application to unload with no exception
ContentPlaceHolder cp = (ContentPlaceHolder)p.Master.FindControl("Content2");
Label label = (Label)cp.FindControl("SomeLabel");
It just unloads itself with no exception mesage. Why does it happen?
I was able to get this to work so that it finds a Label from within a ContentPlaceHolder where my user control resides:
Site1.Master:
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" MasterPageFile="~/Site1.Master" %>
<%# Register Src="WebUserControl1.ascx" TagName="stuff" TagPrefix="uc" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="Label1" Text="Label" runat="server" />
<uc:stuff ID="test" runat="server" />
</asp:Content>
WebUserControl1.ascx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.Master != null)
{
Control c = Page.Master.FindControl("ContentPlaceHolder1");
if (c != null)
{
Label l = (Label)c.FindControl("Label1");
}
}
}

Resources