<%= Session.Item("user_fullname")%>
this supposed to display session value right?
but mine is displaying code itself....
Note: I have included aspx page in another aspx, like:
<% Response.WriteFile("../etc/header1.aspx") %>
the displaing session code is inside header1.aspx
anyone knows how to display session value?
Session items would be stored as an Object. You need to cast to the correct type:
<%= (string)Session.Item("user_fullname") %>
When using Response.WriteFile("../etc/header1.aspx"), the usual Page Life cycle will NOT be executed for the mentioned header1.aspx page.
Since there will be no asp.net page life cycle processing , the inline code
<% %> won't be executed at all and therefore all such statements will be displayed as it is.
Response.WriteFile() is used to write just the Contents of a file, which can contain HTML , Controls, directly to the output stream.
Related
While trying to insert controls in my page with the following block of code, i got this exception
The Controls collection cannot be modified because the control
contains code blocks
Literal lite=new Literal();
lite.Text = #"<div class='cookieLaw_slidesharey'><a id='cookieLaw_slideshare' href='#'><img src='/img/content/cookielaw_slideshare.jpg'/></a></div>";
Control control = this.FindControl("PlaceHolder");
control.Controls.AddAt(0,lite);
I tried to follow the advices written in this thread:
"The Controls collection cannot be modified because the control contains code blocks"
Which are basically:
replacing <%= %> with <%# %>
And adding
Page.Header.Databind() in the page load
But i get the following exception:
Object reference not set to an instance of an object.
Thank you for your help.
PS: i'm using .NET 3.5
Check if your "PlaceHolder" has a runat=server attribute. That should already help. You can address that control by it's id then instead of using FindControl.
The reason why your Page.Header.Databind() is not working is probably because you don't have a head section with the runat=server like in the example you copied from. They were adding controls in the header there which can be slightly different.
Alrighty to make this quick:
I want to set start and end dates for a calendar extender dynamically on-change or on page load
the values are put into hidden fields on the .ascx and populated during page load in an if not postback
one set of calendar extenders is in the item template field of a grid view call this set A
the others are in a normal html table - set b
set a and set b have flags StartDate="<%# hfStart.value%>" EndDate="<%# hfEnd.value%>"
set a in the item template of a grid view column works like a charm
set b in the HTML table doesn't appear to work at all
What gives?
So far I have tried other server tags with the same code inside but I am obviously missing the salient detail. Why does one work and not the other?
UPDATE: Tried
CDate(hfstart.value).ToString with <%: and <%= tags
<%= hfstart.value %>
Unless I misunderstand, <%= will fire at the very END of the asp.net life cycle stopping it from being useful in this context.
As it turns out you DO need to use <%# %> within asp tags as others like <% %> and <%= %> execute at the end of the ASP.NET life cycle and get spit out the buffer to god knows where. When using <%# %> however, the asp control needs to be DataBound(); at the appropriate time.
This happens automatically for controls modeled in the <item template> tags in the gridview because everything within the gridview is bound on its gridview.DataBound() command.
Could it be because you're using the <%# %> tags which are for data binding? This would explain why they work in the GridView, because it supports data binding.
However in a basic HTML table you should use <% %> tags instead, or <%= %> to call a method.
For full details of the tag types, try this reference.
I am a asp .net beginner. I want to use some objects created at Site.Master.cs in Site.Master. Is there an easy way to do it?
I know how to do it in MVC(by using view(the object)). But how can i do it in normal ASP .net web application?
I don't understand what exactly you want to do.
If you want to insert some string into tag's title you can insert the following thing in SiteMaster.master file:
<img src="<%= Page.ResolveUrl("~/") %>images/logo.png">
instead of:
<img src="images/logo.png">
In the first case there will be calculated the path from the root of your application. In the second case there will be relative link. This is because server will CALCULATE the value of Page.ResolveUrl("~") function and will WRITE it in src tag.
You can do the same thing with any other methods, classes if you defined them properly. But I wouldn't recommend you to implement complicated logic in .aspx files (or .master files). Because you can end up with many difficulties with testing and styling such application.
There are other server tags:
<% %> - an embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. Description and examples
<%= %> - most useful for displaying single pieces of information. Description and examples
<%# %> - data binding expression syntax. Description and examples
<%$ %> - ASP.NET Expression. Description and examples
<%# %> - Directive Syntax. Description and examples
<%-- --%> - Server-Side Comments. Description and examples
<%: %> like <%= %> - But HtmlEncodes the output (new with Asp.Net 4). Description and examples
Another way: you can use JSON to send some data to the client and then process it with javascript. Take a look at this project.
If the #Page directive in your .aspx file has Inherits="XYZ" where XYZ is the class declared in your .cs file, you can simply add a protected field to your class and assign a value to it. You'll be able to access it in the .aspx file just by using its name.
You can also use HttpContext.Items property to keep objects during a single request:
HttpContext.Current.Items["SavedItem"] = "hello world";
And use it in page:
<%= ((string)Context.Items["SavedItem"]) %>
Any public or protected property or method in Site.Master.cs will be accessible from Site.Master.
but how to invoke c# code in aspx ?
There are several ways, including the <%= %> construction, and databinding syntax.
It would help if you explained what you're trying to achieve.
I have a master page and I want to add a placeholder twice - so that I have the same placeholder in two places of the master page so that actual page just specifies content of the placeholder once and that content is rendered twice on the resulting page. The goal is to avoid duplication of content.
If I try to add a placeholder with the same id twice it won't compile - ASP.NET doesn't like that.
How do I achieve that? What are other options?
Place your placeholder in an Action, and call it where necessary.
<%
Action myPlaceholder = () =>
{%>
<asp:ContentPlaceHolder ID="X" runat="server" />
<%}
%>
...then call wherever necessary in code.
<% if (conditionMet)
myPlaceholder(); >%
The error occurs at compile time: a complaint will be made if placeholders with the same ID exist. This approach clears that hurdle, and gives a lot of flexibility.
I think you are probably looking for a user control:
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
With User Controls you can add your markup and code behind into an .ascx and then call this in your Master Page/Web Form multiple times, therefore avoiding duplicate code.
I haven't tried this, but you could lookup the contents of the placeholder you are interested in duplicating, and copying it in your code behind. This post: http://programcsharp.com/blog/archive/2009/01/22/test-if-masterpage-contentplaceholder-has-content-or-is-empty.aspx
has some good insight into manipulating master pages.
<script ID="clientEventHandlersVBS" LANGUAGE="vbscript">
s=pass()
y=s
</script>
<%
session("password")=y
Response.write(session("password"))
Response.write(y)
%>
i have this code. but nothing is getting stored inside the session variable neither anything is getting printed. cant i access the variables declared outside the asp code or is their any syntax mistake. any help is really appreciated
First of all put
<% Option Explicit %>
at the top of every .asp page.
You will immediatly see that you are trying to access non declared variabels s and y.
So of course nothing is stored in the session variables.
Can you not use
<%
s = pass
y = s
%>
and so on ?
What is the purpose of the <script ... line if you are using vbscript any how ?
As implied by the ID of the script (clientEventHandlersVBS) the code contained in there refers to the client (the browser, IE in this case since it is the only one that supports VB client-side)
the <% %> tags though refer to server side ASP code..
These two can never communicate as they happen at different times/computers...