Inline Expression <%= %> in html attribute - c#

I'm working on trying to implement a simple cache work-around for a project.
In the project I have a public string:
public string noCache = DateTime.Now.Second.ToString();
I'm trying to bring this in at the end of the css reference so it prevents pulling the cached version.
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>">
This would make the url "./css/core.css?version=<%= noCache%>" literal and not plug in the variable. After some reading I've found out that Inline Expressions aren't supported in .net. The weird thing is, before I figured that out I did this:
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>" <%= noCache%> />
For whatever reason it works when I use this. Can someone explain to me why and how it'd be best to set something like this up without weird workaround.
AppSite.Master:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="AppSite.master.cs" Inherits="APP_Partnership.AppSite" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%# noCache%></title>
<link runat="server" id="CSS1" rel="stylesheet" href="<%= noCache%>"/>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<div class="header">
APP SITE
</div>
<div class="content">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
AppSite.Master.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace APP_Partnership
{
public partial class AppSite : System.Web.UI.MasterPage
{
public string noCache = DateTime.Now.Second.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
}
}

I would use a UserControl to do that.
Here the code for the usercontrol ascx page:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CacheControl.ascx.cs" Inherits="Usercontrols_CacheControl" %>
<link rel="stylesheet" href="./css/core.css?version=<%= noCache%>">
The code behind file:
protected string noCache = DateTime.Now.Second.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
Adding to the Masterpage:
<%# Register TagPrefix="MyTagName" TagName="CacheControl" Src="~/Usercontrols/CacheControl.ascx" %>
and in the head section:
<MyTagName:CacheControl ID="CacheControl" runat="server" />
The output on the page in the header looks like that:
<link rel="stylesheet" href="./css/core.css?version=3">
Hope this helps.

Related

SelectedNodeChanged of the treeview control not working after putting it inside a jQuery tab?

I am developing a website using ASP.NET and used jQuery to implement the tabs in the page.
I got the design perfectly. but problem is I have a treeview on each tab. When user click a node the page should redirect to another page. Currently I am using response.redirect method which worked perfectly. But after I put the treeview control inside this jQuery tab post back occurs but instead of redirecting again it loads the same page with the tabs. I put a break point on SelectedNodeChanged event of the treeview but compiler even didn't reach to the breakpoint. What went wrong?
Here is the code
<link rel="stylesheet" href="Content/jquery-ui.css">
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/jquery-ui.js"></script>
<script>
$(function () {
$("#tabs").tabs();
});
</script>
<div id="tabs">
<ul>
<li>Test1</li>
</ul>
<div id="tabs-1">
<asp:TreeView ID="tvTest" runat="server" OnSelectedNodeChanged="tvTest_SelectedNodeChanged">
<NodeStyle CssClass="tree" />
</asp:TreeView>
</div>
</div>
Here is the selectednodechanged event
protected void tvTest_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Redirect("~/Display.aspx",true);
}
These are my Test pages. When the node changes, I.E. I click on a different node, it redirects me to the said page.
TreeView.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TreeTest.aspx.cs" Inherits="WebApplication.TreeTest" %>
<!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>
<link rel="stylesheet" href="Scripts/jquery-ui.css" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#tabs").tabs();
});
</script>
</head>
<body>
<form runat="server">
<div id="tabs">
<ul>
<li>Test1</li>
</ul>
<div id="tabs-1">
<asp:TreeView ID="tvTest" runat="server" OnSelectedNodeChanged="tvTest_SelectedNodeChanged">
<NodeStyle CssClass="tree" />
<Nodes>
<asp:TreeNode Text="Test1"></asp:TreeNode>
<asp:TreeNode Text="Test2"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
</div>
</form>
</body>
</html>
TreeView.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class TreeTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void tvTest_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Redirect("~/Display.aspx", true);
}
}
}

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

I need a textbox value in form 1 to be inserted into form2 as a string in the html level in asp

I would like to 'transfer' a textbox value into a html code. E.g the textbox value is a website like http://www.google.com in form1.aspx. and i would like the value to be put into the html code in form2.aspx . Might i ask how i should be going around on this?
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
Like i would like the textbox1 value to replace the current www.google.com
Here's how you can do this:
Webform1 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SOTest1.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:Label ID="Label1" runat="server" Text="Your text: ">
</asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Webform1 Code:
using System;
namespace SOTest1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string myString = Server.HtmlEncode(TextBox1.Text);
Response.Redirect("Webform2.aspx?mystring=" + myString);
}
}
}
Webform2 Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SOTest1.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="logoposition">
<img src="Logo.jpg" height= "120"; width="280"; />
</span>
</div>
</form>
</body>
</html>
Webform2 Code:
using System;
namespace SOTest1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mystring = Request.QueryString["mystring"];
myAnchor.HRef = mystring;
myAnchor.InnerText = mystring;
}
}
}

Only Content Controls are Allowed (C# webcontrols)

So, I'm not sure what's going on. My boss wasn't pleased with me using MVC and Razor, so I'm being forced to code with this nasty webcontrol/codebehind style. =/
The error is:
Only Content controls are allowed directly in a content page that contains Content controls.
Here is the masterpage:
<%# Master Language="C#"%>
<!DOCTYPE html>
<html>
<head>
<title>Application Form</title>
</head>
<body>
<div id="container">
<asp:contentplaceholder id="contentPlaceHolder" runat="server" />
</div>
</body>
</html>
And here is the Default.aspx page that's throwing the error.
<%# Page Language="C#" Inherits="dumb.Default" MasterPageFile="MasterPage.master" %>
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<p>Please complete this application.</p>
<form action="Default.aspx" method="post" runat="server">
<div>
<span class="spaced">Name:</span><asp:TextBox id="name" runat="server" />
</div>
</form>
<p>
<asp:Label id="finalmessage" runat="server" />
</p>
</asp:Content>
And the silly Default.aspx.cs codebehind...
using System;
using System.Web;
using System.Web.UI;
namespace dumb
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (IsPostBack) {
finalmessage.Text = "Submission Processed Successfully!";
}
}
}
}
All of your content, including static HTML must be inside Content tags in the content page. You have a <h2> outside:
<h2>Application Form</h2>
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
Should be:
<asp:Content id="content" ContentPlaceHolderID="contentPlaceHolder" runat="server">
<h2>Application Form</h2>

passing value from ASP.NET to Java Scripts to HTML

I need value to be passing from ASP.NET page to JavaScript and then to HTML textfield.
My following code is able to read values from ASP.NET to JavaScript but unable to pass value form JavaScript to HTML TextField. Can anybody please correct my code.
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function can() {
var myVariable = '<%=ServerSideVariable %>';
document.write(myVariable);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" value='can()'/>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
public string ServerSideVariable { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
string pval = "Passed value;
ServerSideVariable = pval;
}
}
document.getElementById('Text1').value = myVariable;
But you need to move the assignment script block bellow the declaration of element Text1, or do it on document.ready
Anyway, you also need to error check.
It's best to use JQuery anyway, and get familiar with it.
As others have said with javascript OR like this:
(since there is nothing in your example that requires javascript -- KISS-- don't use javascript if you don't have to do so.)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<input id="Text1" type="text" ><%=ServerSideVariable %></input>
</div>
</form>
</body>
</html>
Do this instead in a document.load.
document.getElementByID('Text1').value = myVariable;

Categories

Resources