I am creating data driven pages using ASP.NET C# and want to dynamically set the page title using code behind
<%# Page Title="" Language="C#" MasterPageFile="~/FLMaster.master" AutoEventWireup="true" CodeFile="legal-expenses-insurance-news-item.aspx.cs" Inherits="legal_expenses_insurance_news_legal_expenses_insurance_news_item" %>
I have tried using the separate title tags lower down in the page but this didn't work either. Can anyone advise how to do this.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyApplication
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Title of my page";
}
}
}
You can modify the Page title like above from your aspx.cs (the code behind file).
If you want to do this directly in your .aspx file, try this:
<% this.Title = "Some Title" %>
This works if you correctly set your Language = "C#" in your #Page directive, which I see you did.
Page class reference from MSDN
The Page has a Title property:
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Title";
}
You should remove the Title="" from the aspx page. It will override the Title set in your code-behind
<%# Page Language="C#" MasterPageFile="~/FLMaster.master" AutoEventWireup="true" CodeFile="legal-expenses-insurance-news-item.aspx.cs" Inherits="legal_expenses_insurance_news_legal_expenses_insurance_news_item" %>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Title = "Title";
}
}
I know that this is an old thread, but I found that Page.Title couldn't always be overriden, but Page.Header.Title could (mostly)... so my solution for dynamic title tags from the Master codebehind is:
if (Page.Header != null)
{
if (Page.Header.Title == null || !Page.Header.Title.Contains("COMPANYNAME"))
{
var otitle = Page.Header.Title;
if (otitle == null || otitle.Length==0) {
var textinfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
otitle = textinfo.ToTitleCase(this.Parent.GetType().Name.Replace("_"," ").Replace("aspx",""));
}
Page.Header.Title = "COMPANYNAME" + " - " + otitle;
}
Page.Header.Title = Page.Header.Title.Replace("COMPANYNAME", Auth.getSetting("companyName"));
}
Related
I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?
public partial class Site : System.Web.UI.MasterPage
{
public string StatusNachricht
{
get
{
return lblStatus.Text;
}
set
{
lblStatus.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
I have tried this, but was unsuccessful in making it work:
public partial class DatenAendern : System.Web.UI.Page
{
var master = Master as Site;
protected void Page_Load(object sender, EventArgs e)
{
if (master != null)
{
master.setStatusLabel("");
}
}
protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//some code
if (master != null)
{
master.setStatusLabel("Passwort erfolgreich geändert.");
}
}
catch (Exception ex)
{
if (master != null)
{
master.setStatusLabel("Passwort konnte nicht geändert werden!");
}
}
}
}
}
In the MasterPage.cs file add the property of Label like this:
public string ErrorMessage
{
get
{
return lblMessage.Text;
}
set
{
lblMessage.Text = value;
}
}
On your aspx page, just below the Page Directive add this:
<%# Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %>
<%# MasterType VirtualPath="Master Path Name" %> // Add this
And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:
this.Master.ErrorMessage = "Your Error Message here";
In Content page you can access the label and set the text such as
Here 'lblStatus' is the your master page label ID
Label lblMasterStatus = (Label)Master.FindControl("lblStatus");
lblMasterStatus.Text = "Meaasage from content page";
It Works
To find master page controls on Child page
Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;
lbl_UserName.Text = txtUsr.Text;
I have a helper method for this in my System.Web.UI.Page class
protected T FindControlFromMaster<T>(string name) where T : Control
{
MasterPage master = this.Master;
while (master != null)
{
T control = master.FindControl(name) as T;
if (control != null)
return control;
master = master.Master;
}
return null;
}
then you can access using below code.
Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null)
lblStatus.Text = "something";
You cannot use var in a field, only on local variables.
But even this won't work:
Site master = Master as Site;
Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:
Site master = null;
protected void Page_Init(object sender, EventArgs e)
{
master = this.Master as Site;
}
This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.
Crucial bit: Master.Master.
See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl
Example:
'Find the content control
Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain")
'now find controls inside that content
Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")
If you are trying to access an html element: this is an HTML Anchor...
My nav bar has items that are not list items (<li>) but rather html anchors (<a>)
See below: (This is the site master)
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="" runat="server" id="liHome">Home</a>
<a class="mdl-navigation__link" href="" runat="server" id="liDashboard">Dashboard</a>
</nav>
Now in your code behind for another page, for mine, it's the login page...
On PageLoad() define this:
HtmlAnchor lblMasterStatus = (HtmlAnchor)Master.FindControl("liHome");
lblMasterStatus.Visible =false;
HtmlAnchor lblMasterStatus1 = (HtmlAnchor)Master.FindControl("liDashboard");
lblMasterStatus1.Visible = false;
Now we have accessed the site masters controls, and have made them invisible on the login page.
In this post I wanted to figure out how to create dynamically created textboxes in C# Visual Studio.
Adding additional textboxes to aspx based on xml
However, when I try to call the ID of these dynamically created textboxes later in my code to figure out what text the user entered into them, I am getting an error that says these IDs do not exist in the current context. Does anyone know how I would be able to call these?
credit to Adding additional textboxes to aspx based on xml
Here is my entire code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace WebApplication4
{
public partial class WebForm15 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsCallback)
{
//credit to https://stackoverflow.com/questions/44076955/adding-additional-textboxes-to-aspx-based-on-xml#comment75336978_44078684
const string xml = #"<Number>
<Num>1</Num>
<Num>2</Num>
<Num>3</Num>
</Number>";
XDocument doc = XDocument.Parse(xml);
int i = 0;
foreach (XElement num in doc.Root.Elements())
{
TextBox box = new TextBox
{
ID = "dynamicTextBox" + i,
Text = num.Value,
ReadOnly = false
};
divToAddTo.Controls.Add(box);
divToAddTo.Controls.Add(new LiteralControl("<br/>"));
i++;
}
}
}
protected void BtnGetValues_Click(object sender, EventArgs e)
{
IList<string> valueReturnArray = new List<string>();
foreach (Control d in divToAddTo.Controls)
{
if (d is TextBox)
{
valueReturnArray.Add(((TextBox)d).Text);
}
}
//valueReturnArray will now contain the values of all the textboxes
}
}
}
Here is aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm15.aspx.cs" Inherits="WebApplication4.WebForm15" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="divToAddTo" runat="server" />
<asp:Button runat="server" ID="BtnGetValues" Text="GetValues" OnClick="BtnGetValues_Click" />
</form>
</body>
</html>
Figured it out!!! Here is what I found after scouring the internet for hours
Solution:
When using dynamic controls, you must remember that they will exist only until the next postback.ASP.NET will not re-create a dynamically added control. If you need to re-create a control multiple times, you should perform the control creation in the PageLoad event handler ( As currently you are just creating only for first time the TextBox using Condition: !IsPostabck ). This has the additional benefit of allowing you to use view state with your dynamic control. Even though view state is normally restored before the Page.Load event, if you create a control in the handler for the PageLoad event, ASP.NET will apply any view state information that it has after the PageLoad event handler ends.
So, Remove the Condition: !IsPostback, So that each time the page Loads, The TextBox control is also created. You will also see the State of Text box saved after PageLoad handler completes. [ Obviously you have not disabled ViewState!!! ]
Example:
protected void Page_Load(object sender, EventArgs e)
{
TextBox txtBox = new TextBox();
// Assign some text and an ID so you can retrieve it later.
txtBox.ID = "newButton";
PlaceHolder1.Controls.Add(txtBox);
}
Now after running it, type anything in text box and see what happens when you click any button that causes postback. The Text Box still has maintained its State!!!
i have built a user control with web controls in ascx page.
//ascx file
<%# Control Language="C#" AutoEventWireup="true" ClassName="Product"CodeFile="Product.ascx.cs" Inherits="Usercontrols_Product" %>
<link href="../StyleSheet/StyleSheet.css" rel="stylesheet" type="text/css" />
<div>
<asp:Panel ID="box" CssClass="productBox" runat="server">
<asp:Image ID="imgProduct" CssClass="productImage" runat="server" /><br />
<asp:Label ID="lblProductName" CssClass="productLbl" runat="server"></asp:Label><br/>
<asp:Label ID="lblProductPrice" CssClass="productLbl" runat="server"></asp:Label>
</asp:Panel>
</div>
in the cs file of user control i created acontructor which get another class reference.
whith the class i get in contructor i want insert data to child controls.
//ascx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Usercontrols_Product : System.Web.UI.UserControl
{
public EventHandler cmdDetailsClick;
protected Product thisProduct;
public Usercontrols_Product( Product pr)
{
thisProduct = pr;
imgProduct.ImageUrl = thisProduct.ImageUrl;
lblProductName.Text = thisProduct.CompanyName + "<br/>" + thisProduct.ProductName + " " + thisProduct.Model;
lblProductPrice.Text = thisProduct.Price.ToString() + " " + "israeli shekel";
}
public Usercontrols_Product()
{
}
protected void cmdContinue_clicked(object sender, EventArgs e)
{
//we send to event product class of sender
if (cmdDetailsClick != null)
cmdDetailsClick(thisProduct, EventArgs.Empty);
}
}
the problem is that now with spesial constructor web controls from aspx file are not being
initiallized. i tried initiallize them in code with new keyword but than they are with no attributes from ascx file. with no special constructor its works fine .but i need this special constructor. how can i initiallize this webcontrols whith their attributes from ascx file
The UserControl is instantiated by Page (i.e. constructor of user control is out of your control) - you can define one, but the default constructor will be called instead. If you want to pass data, there are numerous techniques how to do so; simplest way:
public Product Product {
set {
thisProduct = value;
imgProduct.ImageUrl = value.ImageUrl;
lblProductName.Text = value.CompanyName + "<br/>" +
value.ProductName + " " + value.Model;
lblProductPrice.Text = value.Price.ToString() + " " + "israeli shekel";
}
}
To clarify how the process work on behind:
.aspx (or .ascx) file are being compiled on runtime by IIS.
So for example if you have simple user control:
<%# Control CodeBehind="FooControl.ascx.cs" Inherits="TestApplication.FooControl" %>
<asp:Literal ID="litBar" runat="server" Text="Whatever" />
IIS will make something like this from your control (shortened):
namespace ASP {
public class foocontrol_ascx : global::TestApplication.FooControl {
private static bool #__initialized;
public foocontrol_ascx() {
((global::WebApplication4.FooControl)(this)).AppRelativeVirtualPath = "~/FooControl.ascx";
if ((global::ASP.foocontrol_ascx.#__initialized == false)) {
global::ASP.foocontrol_ascx.#__initialized = true;
}
}
private global::System.Web.UI.WebControls.Literal #__BuildControllitBar() {
global::System.Web.UI.WebControls.Literal #__ctrl;
#__ctrl = new global::System.Web.UI.WebControls.Literal();
this.litBar = #__ctrl;
#__ctrl.ID = "litBar";
#__ctrl.Text = "Whatever";
return #__ctrl;
}
private void #__BuildControlTree(foocontrol_ascx #__ctrl) {
global::System.Web.UI.WebControls.Literal #__ctrl1;
#__ctrl1 = this.#__BuildControllitBar();
System.Web.UI.IParserAccessor #__parser = ((System.Web.UI.IParserAccessor)(#__ctrl));
#__parser.AddParsedSubObject(#__ctrl1);
}
protected override void FrameworkInitialize() {
base.FrameworkInitialize();
this.#__BuildControlTree(this);
}
}
}
Notice the built class inherits from your code behind and takes care about the markup as well (see how it creates literal litBar and set text to "Whatever"). Also this class has only one constructor - default constructor (so it naturally calls default constructor from class in code behind). I.E. - you can define custom constructor for your control but you have to provide default constructor anyway (so the built can work with it) and the built class ignores it (as the IIS can have no clue what parameters you want to pass to your custom constructor).
I am trying to create a basic macro that encapsulates a form and the ability to email the details entered into it. It works fine for Textboxes, but for some reason the DropDownLists take the first value in the list of options. I've been working on this for hours and seem to've tried everything, so hopefully someone can suggest a solution. I am using Umbraco 4.0.3 and unfortunately upgrade is not an option. My reduced code is as follows:
CorpRefundForm.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CorpRefundForm.ascx.cs" Inherits="umbracowebsitewizard_site.Usercontrols.CorpRefundForm" %>
<asp:DropDownList ID="frm_dropdown" runat="server" CssClass="linkselect" />
<br />
<button id="submitButton" runat="server" onserverclick="submitButton_Click">Submit</button>
CorpRefundForm.ascx.cs
using System;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace umbracowebsitewizard_site.Usercontrols
{
public partial class CorpRefundForm : UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
frm_dropdown.Items.Add(new ListItem("Select one", ""));
frm_dropdown.Items.Add(new ListItem("One", "One"));
frm_dropdown.Items.Add(new ListItem("Two", "Two"));
frm_dropdown.Items.Add(new ListItem("Three", "Three"));
frm_dropdown.Items.Add(new ListItem("Four", "Four"));
}
}
public void submitButton_Click(object sender, EventArgs e)
{
SmtpClient mySMTPClient = new SmtpClient();
mySMTPClient.Send("[email removed]", "[email removed]", "Test", frm_dropdown.SelectedValue + frm_dropdown.Text + frm_dropdown.SelectedItem.Value + frm_dropdown.SelectedItem.Text);
}
}
}
CorpRefundForm.ascx.designer.cs:
using System.Web.UI.WebControls;
namespace umbracowebsitewizard_site.Usercontrols
{
public partial class CorpRefundForm
{
protected DropDownList frm_dropdown;
}
}
That probably happens since your first ListItem does not have a Value assigned. Try to assign a value, and see if it works.
frm_dropdown.Items.Add(new ListItem("Select one", "-1"));
Solved it! Turns out the jquery.linkselect-1.2.07.min.js library was doing something with the DropDownList that broke it. Took that out and it worked.
I have masterpage which has runat="server" & Id set on body tag. see below
<body id="MasterPageBodyTag" runat="server">
code behind on masterpage I've added the following code:
public HtmlGenericControl BodyTag
{
get { return MasterPageBodyTag; }
set { MasterPageBodyTag = value; }
}
now I want to add css class to body tag from Class1.cs file in App_code folder.
On the .aspx am passing the master page control using the following code:
protected void Page_Load(object sender, EventArgs e)
{
backend.FindPage((PageTemp)this.Master);
}
Now on Class1.cs I have the following
public static void FindPage(Control mp)
{
Page pg = (Page)HttpContext.Current.Handler;
PropertyInfo inf = mp.GetType().GetProperty("BodyTag");
}
I want to add the following to found BodyTag
// BodyTag.Attributes.Add("class", "NewStyle");
But can't seem to find a way to add atrribute or cast the inf to HtmlGenericControl.
Any help would be great.
Rather than having a dependency on the Master Page type, I'd simply use FindControl to search for the body element by Id. Assuming the body tag is on your top-level Master page, and also assuming you may be using nested master pages, it would look something like:
private static MasterPage GetTopLevelMasterPage(Page page)
{
MasterPage result = page.Master;
if (page.Master == null) return null;
while(result.Master != null)
{
result = result.Master;
}
return result;
}
private static HtmlGenericControl FindBody(Page page)
{
MasterPage masterPage = GetTopLevelMasterPage(page);
if (masterPage == null) return null;
return masterPage.FindControl("MasterPageBodyTag") as HtmlGenericControl;
}
private void UpdateBodyCss()
{
HtmlGenericControl body = FindBody(this);
if(body != null) body.Attributes.Add(...);
}
You could even remove the dependency on the id by searching for an HtmlGeneric control with a tag name of "body":
private static HtmlGenericControl FindBody(Page page)
{
MasterPage masterPage = GetTopLevelMasterPage(page);
if (masterPage == null) return null;
foreach(Control c in masterPage.Controls)
{
HtmlGenericControl g = c as HtmlGenericControl;
if (g == null) continue;
if (g.TagName.Equals("body", StringComparison.OrdinalIgnoreCase)) return g;
}
return null;
}
You need the add the following on the aspx file:
<%# MasterType VirtualPath="~/Your_Master_Page.Master" %>
and then you can do at the .cs of your page:
Master.BodyTag.Attributes.Add("class", "NewStyle");
What you're doing seems a bit convoluted and there are probably easier ways to deal with this.
To answer your question, you don't need to use reflection. You can simply cast the parameter you're passing to FindPage to the master page type you've created.
You haven't specified the type name of your master page so I'll give it the name MyMasterPage.
So FindPage should look like:
public static void FindPage(Control mp)
{
var masterPage = mp as MyMasterPage;
if (mp != null)
{
mp.BodyTag.Attributes.Add("class", "NewStyle");
}
}