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");
}
}
}
Related
I have the following code, in which I am trying to transfer some text in a previous page's textbox control (located in the master page) to the same master page textbox control. However it is not working. Please let me know if you can spot the error.
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox placeholder =
(TextBox)PreviousPage.Master.FindControl("TextBox1");
if (placeholder != null)
{
TextBox searchBox = (TextBox)Master.FindControl("TextBox1");
string search = placeholder.Text.ToString();
searchBox.Text = search;
}
}
}
this is the .aspx of the master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="AlexBookShop.Site1" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.auto-style1 {
margin-bottom: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">Children</asp:ListItem>
<asp:ListItem Value="1">Finance</asp:ListItem>
<asp:ListItem Value="3">Non-Fiction</asp:ListItem>
<asp:ListItem Value="4">Technical</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style1"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Log Out" OnClick="Button2_Click" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
For transferring data from one page to other page via MasterPAge you need to do cross page posting.
you need to assign the PostBackUrl property of a button control on the first page to point to the second one. like
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Secondpage.aspx"/>
set the PreviousPage directive on the second page:
<%# PreviousPageType VirtualPath="~/firstpage.aspx" %>
Then whenever second page receives a postback, get the data you need from the first page:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox placeholder =
(TextBox)PreviousPage.Master.FindControl("TextBox1");
if (placeholder != null)
{
TextBox searchBox = (TextBox)Master.FindControl("TextBox1");
string search = placeholder.Text.ToString();
searchBox.Text = search;
}
}
}
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);
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)
I get an "Object reference not set to an instance of an object." error on the line "welcome.Text = ...."
Home:
protected void OKButton_Click(object sender, EventArgs e)
{
if (UserNameTextBox.Text != String.Empty)
{
Session["UserName"] = UserNameTextBox.Text;
Label welcome = (Label)Master.FindControl("GreetingLabel");
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
}
}
<%# Page Title="" Language="C#" MasterPageFile="~/Professional.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<br /><br />
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
<br /><br />
<asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem Text="Professional" Value="Professional"></asp:ListItem>
<asp:ListItem Text="Colourful" Value="Colourful"></asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button ID="OKButton" runat="server" Text="OK" onclick="OKButton_Click" />
</asp:Content>
I got the code from MCTS Exam 70-515 Web Dev book.
I looked at the Errata page, no luck. http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780735627406
Master Page:
public partial class Professional : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null)
{
GreetingLabel.Text = String.Format("Welcome, {0}!", Session["UserName"]);
}
}
}
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Professional.master.cs" Inherits="Professional" %>
<!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></title>
<asp:ContentPlaceHolder id="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<link href="~/Styles/Site.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Contoso.gif" /><asp:Label ID="Label1" runat="server" Text="Welcome to Contoso!"
Font-Size="X-Large"></asp:Label>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Products" Value="Products"></asp:MenuItem>
<asp:MenuItem Text="Services" Value="Services"></asp:MenuItem>
<asp:MenuItem Text="Downloads" Value="Downloads"></asp:MenuItem>
<asp:MenuItem Text="About Us" Value="About Us"></asp:MenuItem>
</Items>
</asp:Menu>
<asp:ContentPlaceHolder id="MainContent" runat="server">
<asp:Label ID="GreetingLabel" runat="server" Text="Label"></asp:Label>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Regards
LF
Where is the GreetingLabel control located in the master? If it's in a ContentPlaceHolder the NamingContainer is that not the master.
You: "it is in a Master, under":
<asp:ContentPlaceHolder id="MainContent" runat="server">
That's it, you first have to find the ContentPlaceHolder, then use FindControl on it:
var cPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
Label welcome = (Label)cPlaceHolder.FindControl("GreetingLabel");
Now you don't get a NullReferenceException on welcome.Text anymore:
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
Edit Since you have commented that it still doesn't work for whatever reason. I will suggest a different - better - approach. Provide a public property in your Master, for example Greeetings. Then you can get/set the Label.Text via this property. That is much more readable and maintainable. It will also work even if you change the label to a different control like TextBox or Div.
For example (in the MasterPage of type Professional):
public string Greetings
{
get { return GreetingLabel.Text; }
set { GreetingLabel.Text = value; }
}
Now you can cast the Master property in your content-page to Professional to access it:
Professional professional = (Professional) this.Master;
professional.Greetings = String.Format("Welcome, {0}!", Session["UserName"]);
Try this:
see this
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);// replace Session["UserName"] with Session["UserName"].ToString()
now your new line is
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"].ToString());
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);
}