I've got a ASP.NET webforms site with some inline code on a master page ..
<meta property="og:title" content="<%=HeadTitle %>"/>
but it's rendering that line as..
<meta property="og:title" content="<%=HeadTitle %>" />
In the codebehind, i have the following ...
protected string HeadTitle { get; set; }
Can anyone help?
Remove the runat="server" attribute from the <head> tag in the master page.
The approach I prefer is to set the value of the meta in the code behind.
protected void myMeta(string myTitle, string myContent)
{
Page.Title = myTitle;
if ((Page.Header != null) && (Page.Header.Controls.Count > 0))
{
Page.Header.Controls.AddAt(1, new HtmlMeta("content", myContent));
}
}
The .aspx itself would just have the normal tags
<head id="myHead" runat="server">
<title></title>
</head>
If it's in a <head> with runat="server", you can either remove the runat="server" part, or you can change <%= to <%# and call Header.DataBind(); from within Page_Load
Related
I have an ASP.Net website with C# code Behind. I'm trying to get the selected Value on a double click from an asp.net List Box. All the solutions I see are for Windows Forms, were there is actually a Doubleclick event. No such luck for the System.Web.UI.WebControls version of the ListBox.
Need the doubleclick to trigger a method in the code behind that allows me to read the selected value.
There are a lot of posts out there on this, but they are not applicable to System.Web.UI.WebControls.ListBox.
Here is a sampling of the code:
.aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="XXX-AddressBook.aspx.cs" EnableViewState="true" Inherits="VRV_AddressBook" %>
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<%-- <meta http-equiv="X-UA-Compatible" content="IE=7, chrome=1"/>--%>
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<style>
</style>
<script type="text/javascript">
</SCRIPT>
<title> Address Book</title>
</head>
<form id="frmAddressBook" runat="server">
<div>
<asp:ListBox ID="lstbxddNames" runat="server" Rows="17" SelectionMode="Multiple"></asp:ListBox>
</div>
</form>
</body>
</html>
Proposed Method from .aspx.cs page, with no way of triggering because there is no doubleclick action in the Web Ap version:
private void lstbxddNames_DoubleClick(object sender, System.EventArgs e)
{
if (lstbxddNames.Items.Count > 0)
{
for (int i = 0; i < lstbxddNames.Items.Count; i++)
{
if (lstbxddNames.Items[i].Selected)
{
string Test = lstbxddNames.Items[i].Value.ToString();
}
}
}
}
Here is how I am loading the ListBox:
DataTable dt_UserRS = new DataTable();
I then load the contents of the Datatable from a SQL Query (Code not shown)
if (Choice.ToUpper() == "GLOBAL")
{
lstbxddNames.DataSource = dt_UserRS;
lstbxddNames.DataTextField = "Name";
lstbxddNames.DataValueField = "Email";
lstbxddNames.DataBind();
}
Here is the solution that i am using. I actually tried this before posting, but I put it in the wrong place in the PageLoad(I put it in the if (!IsPostBack) section in error).
protected void Page_Load(object sender, EventArgs e)
{
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "lstdbclick")
{
lstbxddNames_DoubleClick(lstbxddNames.SelectedValue.ToString());
// string TEST = lstbxddNames.SelectedValue.ToString();
}
lstbxddNames.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(lstbxddNames, "lstdbclick"));
if (!IsPostBack)
{
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString(); //PART OF PAGE REFRESH DETECTION
Session["SessionId"] = ViewState["ViewStateId"].ToString();
Made sure it would hit the method as well:
private void lstbxddNames_DoubleClick(string Value)
{
if (!String.IsNullOrEmpty(Value))
{
string Test = Value;
}
}
What is happening here?
One thing I don't understand is why were adding the attribute? If I can detect the doubleclick with the event argument, why am I adding the attribute?
In your listbox loop you can wire the double click event to each item. e.g.
item.Attributes.Add("ondblclick", "functionCall();");
I want to add the server name in my page head section dynamically like
<head>
<!-- DP2-WEB005 -->
</head>
can anyone please let me know how can I add this <!-- DP2-WEB005 --> tag in head section.
server name I will handle it but I don't know how add that commented tag dynamically.
HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);
I hope this helps ...
(the reference)
UPDATE:
This is that you want :
string serverName = "QWERTY123";
this.Page.Header.Controls.Add(new LiteralControl("<!-- " + serverName + "-->"));
And here is the output :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title><!-- QWERTY123--></head>
<head runat="server">
<%= serverName %>
</head>
In code behind
public string serverName{get;set;}
protected void Page_Load(object o, EventArgs e)
{
serverName="assign";
}
Try like this
Aspx:
<html>
<head runat="server">
<%= Content %>
</head>
<body>
//Code here
</body>
</html>
Code Behind:
In Code behind write the following code in PageLoad()
public string Content{get;set;}
protected void Page_Load(object sender, EventArgs e)
{
String Content = "Content here";
}
If you want to add css or java-script in your page section you can use the following
var myHtmlLink = new HtmlLink { Href = #"filepath" };
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
Page.Header.Controls.AddAt("0", myHtmlLink);
Having some trouble referencing a Literal in a user control that is included in a Master Page. I'm testing using the following pages (paths probably included unnecessarily...):
~/_inc/header.ascx
~/master_pages/partner_header_footer.master
~/credit_check/test_page.aspx
header.ascx (relevant content)
<%# Control Language="C#" AutoEventWireup="true" CodeFile="header.ascx.cs" Inherits="_inc_header" %>
<link type="text/css" rel="stylesheet" href="/css/header.css" />
<div id="header" class="content-header cf">
<div id="logo"></div><asp:Literal id="contentTitle" runat="server" Text="Customer Service" />
</div>
partner_header_footer.master (relevant content)
<%# Master Language="C#" AutoEventWireup="true" CodeFile="partner_header_footer.master.cs" Inherits="master_pages_partner" %>
<%# Register Src="~/_inc/header.ascx" TagPrefix="uc1" TagName="header" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Wireless</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:header runat="server" ID="header" />
<asp:ContentPlaceHolder id="Wireless" runat="server">
</asp:ContentPlaceHolder>
</form>
</body></html>
test_page.cs (relevant content)
protected void Page_Load(object sender, EventArgs e)
{
Literal mpLiteral = (Literal)Master.FindControl("contentTitle");
if (mpLiteral != null)
{
mpLiteral.Text = "Customer Service Home";
}
}
Not sure what I'm doing wrong, so hopefully someone can point out the error(s) of my ways...
Since the Literal is in a UserControl that is inside the Master Page, Master's FindControl won't be able to find it.
If you want to make the UC's Literal available to the Master, create a Property in the UC's code-behind:
public string MyLiteral { get{ return contentTitle.Text; } set{ contentTitle.Text = value; } }
Then, in your Master Page, you can access header.MyLiteral to set/get the value.
Now, if you want to make it accessible to Child Pages, you'd then expose it again as a Property, but this time in the Master's code-behind:
public string HeaderLiteral { get{ return header.MyLiteral; } set{ header.MyLiteral = value; } }
Finally, in the Child Page, you'll need to cast this.Master to the type of your Master Page (I think it's master_pages_partner based on your MP markup):
var castedMaster = (master_pages_partner)this.Master;
if(null != castedMaster)
{
castedMaster.HeaderLiteral = "Customer Service Home";
}
I want to get HTML DIV content via asp.net C# code behind event.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="WebApplication1.Report.Report" %>
<!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>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#_Hidden_CrystalReportContent').hide();
$('#_Hidden_CrystalReportContent').html("<b>I want to get Current value. 1<sup>st</sup></b>");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="_Hidden_CrystalReportContent">I want to get Current value.</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
My code behind file as below.
public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void Button1_Click(object sender, EventArgs e)
{
string s = Request["_Hidden_CrystalReportContent"].ToString();
}
}
But I still cannot get div content value.
Please let me get your suggestion.
Make the div runat="server" to access on server.
Html
<div id="_Hidden_CrystalReportContent" runat="server">
Code behind
string htmlOfDiv = _Hidden_CrystalReportContent.innerHTML;
Javascript
$(document).ready(function () {
$('#<% _Hidden_CrystalReportContent.ClientID %>').hide();
$('#<%= _Hidden_CrystalReportContent.ClientID %>').html("<b>I want to get Current value. 1<sup>st</sup></b>");
});
Making a div server accessible by puttin runat="server" attribute cause the changed client id if CLientIDMode is not static. You will need to use ClientID attribute to get client id of div in javascript.
Edit: based on comments. You are trying to get the updated html, if so you then you wont get it as on post back only html form elements are posted. Put the changes in some hidden field and assess it on server.
In html
<input type="hidden" id="hdnDivContents" runat="server">
In javascript
$('#<% hdnDivContents.ClientID %>').val("<b>I want to get Current value. 1<sup>st</sup></b>");
In code behind
_Hidden_CrystalReportContent.innerHTML = hdnDivContents.Value;
I am have a masterpage wherein the meta tags name and description are defined. I cannot make the head use runat="server" as I am having some issue with URL routing, if I do so.
I want to dynamically change the meta description of one of the child pages in the website. I have tried this:
HtmlHead headTag = (HtmlHead)this.Header;
HtmlMeta pageMetaTag = new HtmlMeta();
pageMetaTag.Name = "Description";
pageMetaTag.Content = "Test";
headTag.Controls.Add(pageMetaTag);
But I get an error on the Add line, saying:
Object reference not set to an instance of an object.
On keeping <head runat="server"></head> for the child page in the head content, the page runs but meta description tag is not overriden. The meta description is the same of that of the master page...
Kindly help me with this issue....
using: ASP.NET 4 in VS2010
Update(Child Page look alike):
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Profile.aspx.cs" Inherits="Profile"%>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<head runat="server"></head>
<style type="text/css" runat="server">
.lnkbtn
{
color: #000080;
cursor:pointer;
}
.lnkbtn:hover
{
color: #800000;
text-decoration: underline;
}
</style>
<link rel="stylesheet" type="text/css" href='<%= ResolveUrl("~/css/back.css") %>'/>
<script type="text/javascript" src='<%= ResolveUrl("~/css/front.js") %>'></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
// contains
// update panel
// webpartmanager
// webpartzones
</asp:Content>
Update (Head gets nested inside head tag)
While using Page.MetaDescription = "Hi How are you"; the following is the page source:
<head>
<titleMaster Page Title</title>
<meta name="Keywords" content="Master page keywords" />
<meta name="description" content="Master page meta description." />
.
.
.
<head><title>Child page Title</title>
<meta name="description" content="Hi How are you" /></head>
.
.
.
</head>
Try this
HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = "google, yahoo";
Header.Controls.Add(keywords);
Make sure you mark
<head runat="server">
In Asp.net 4.0 you can do this:
protected void Page_Load(object sender, EventArgs e)
{
Page.MetaDescription = "My page - a great page indeed";
Page.MetaKeywords = "keyword1, two, three";
}
I think you need
<head runat="server">
AND
Header.Controls.Add(pageMetaTag);
well at least I use this exact code and it works
HtmlMeta siteVerificationMeta = new HtmlMeta();
siteVerificationMeta.Name = "xxxx";
siteVerificationMeta.Content = "xxxx";
Header.Controls.AddAt(0, siteVerificationMeta);
EDIT: I think this is what you need, you are adding a new header, you need to edit as below
Please note the commented out line!!
//HtmlHead headTag = (HtmlHead)this.Header;
HtmlMeta pageMetaTag = new HtmlMeta();
pageMetaTag.Name = "Description";
pageMetaTag.Content = "Test";
Header.Controls.Add(pageMetaTag);