How to Add Text Dynamically in Head Section in asp.net - c#

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);

Related

Why I get error when I try to use variable of form class from designer?

In code behind I have property called ReportFeatures and Page_Load event:
public partial class FeatureList : System.Web.UI.Page
{
protected string ReportFeatures;
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable<ReportFeature> featureProps = fim.getFeatureProperties();
ReportFeatures = featureProps.ToJson();
}
}
In designer I tried to access ReportFeatures variable:
<head runat="server">
<title></title>
<script type="text/javascript">
window.reportFeatures = <%= ReportFeatures%>;
</script>
</head>
When page loaded I get this error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Any idea why I get that error, and how to fix it?
Instead of using <%= ... %> block, try using data binding expression syntax (<%# ... %>), because <%= ... %> implicitly calls Response.Write() method in Page.Header which counts as code block while data binding expression doesn't count:
<head runat="server">
<title></title>
<script type="text/javascript">
window.reportFeatures = <%# ReportFeatures %>;
</script>
</head>
Then add Page.Header.DataBind() method in Page_Load event, because you want to bind ReportFeatures inside <head> tag which contains runat="server" attribute:
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable<ReportFeature> featureProps = fim.getFeatureProperties();
ReportFeatures = featureProps.ToJson();
// add this line
Page.Header.DataBind();
}
More details about this issue can be found here.

Adding Items to Combobox manually from aspx page ,along with items added from codebehind

I have a problem here
I have year drop down list in my page, for that i am binding items from code behind,The maximum year i am adding from code behind is 2027.
But one user came up ,who wants to select year 2040, I am wondering whether I can manually add year 2040 from aspx page, so that no need to deploy my code.
Please help on this.
Many Thanks
It is not a very good way but you can do this using javascript. below is the code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function myFunction() {
var x = document.getElementById("DropDownList1");
var option = document.createElement("option");
option.text = "2040";
x.add(option);
}
</script>
</head>
<body onload="myFunction();">
<form id="form1" runat="server">
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</form>
</body>
</html>
cs code just to show that drop down is some element of dropdown filled in server side code
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = new int[] { 1, 2, 3 };
DropDownList1.DataBind();
}

How to use asp:image in code behind

I have a problem with rendering asp:image in code behind.
First I explain my method:
In Default.aspx I just use one label.
In code behind I make a string variable and fill it, then I fill the lable.Text by my string variable.
this is my Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
and this is my form load function in Defaul.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = #"<asp:Image ID='Image1' runat='server' ImageUrl='~/images/sc/tiraje.jpg' />
<br/>
<img src='../images/sc/tiraje.jpg' />
";
}
Now this code must render two images. But first image used by asp:image doesn't work! I need to use asp:image in my string variable because my URL changes my result, for example if my URL is:
http://localhost:19551/website/test/Default.aspx
When I give it one "/":
http://localhost:19551/website/test/Default.aspx/
The second URL causes to change the src of second image in my string that I use in <img> tag. so that I want to use asp:image because it uses "~/" for src (imageUrl) that never changes by change URL!
The Literal control introduced by Abdullah ELEN is the easiest and it works. Like this:
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
And then in the code behind, assuming you have already captured the image source into a local variable (photo_src), you add this:
Literal1.Text += "<img src=" + '"' + photo_src + '"' + "/>";
You cannot create in this way because Asp:Image is a server object. I have prepared for you a simple example below.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="pageDefault" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="literalControl" runat="server" />
</div>
</form>
</body>
</html>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
// Write here your SQL query for image URLs..
while(reader.Read())
{
literalControl.Text +=
"<img src=\"../images/sc/" + reader[0].ToString() + ".jpg\" /><br/>";
}
}
You shouldn't be using a label to render the images. Instead place two asp:image controls in the web form or use a placeholder control, such as:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Application.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image runat="server" ID="Image1" />
<br />
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Then you can dynamically create a new image control and set the image urls through codebehind by using their ImageUrl properties:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = "../images/sc/tiraje.jpg";
Image Image2 = new Image();
Image2.ImageUrl = "../images/sc/tiraje.jpg";
PlaceHolder1.Controls.Add(Image2);
}
}

get html DIV content from asp.net c# codebehind event

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;

Old ASP.NET webform has some inline code getting encoded :(

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

Categories

Resources