This question already has answers here:
how to Embedded Code Blocks in ASP.NET Web Pages?
(2 answers)
Closed 8 years ago.
I am a beginner level programmer. I was using C# variable in the aspx page.
I have seen the usage of <% in the aspx page alot.
I need the detail of when to use <% in what requirement like
<% 'When to use this?' %>
<%= 'When to use this?' %>
<%# 'When to use this?' %>
<%# 'When to use this?' %>
I am searching for a useful link regarding this but did not found any help
I hope this could be Helpful.......
http://www.codeproject.com/Articles/384425/Server-side-Delimiters-in-ASP-NET`
You can search with name "delimiter in asp.net" probably google will give you lot of results.
<% %> is server code that executes during the page's render phase which can execute the statements written inside the block which help in interacting with server side at runtime.
<% { Response.Write("Hello !"; }%>
and like wise if you have script function in your page and you want to call that function you can ue this
<% =Callfunc()%>
And by default in all pages and user controls you can see directives. more here
MSDN
<% 'When to use this?' %> Similar to classic ASP and is used to add server-side code within your ASPX page such as:
<% for (int i=0; i < 10; i++) { %>
<p>I am added to the page 10 times</p>
<% } %>
<%= 'When to use this?' %> Similar to the example above only the = allows you to "inject" or reference an expression or variable and not a chunk of code. Belows example refers to MyAnchor which can be declared in the code behind. <a href='<%= MyAnchor %>'></a>
<%# 'When to use this?' %> This is used for page and control declarations: <%# Page Language="vb" AutoEventWireup="false"
<%# 'When to use this?' %> This is used for databinding
<asp:GridView ID="gvMyGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="E-mail" SortExpression="Email">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Email").ToString()%>' NavigateUrl='<%#Eval("Email", "mailto:{0}").ToString() %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Related
I am integrating Asp.net User control(.ascx) in my Mvc Layout page i.e is master page and i am using razor engine.
Error:Error executing child request for handler
'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper ascx.
Control '1_hdfData' of type 'HiddenField' must be placed inside a form tag with runat=server.
This is my code:
_Layout.cshtml:
#Html.Partial("~/UserControls/Data.ascx")
Data.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
Data.ascx.cs:
namespace MyNameSpace.Data
{
public partial class Data : ViewUserControl
{
//Page_Load events and other code
}
}
Is it like if i am inheriting User Control(.ascx) from ViewUserControl so i cant use asp.net control?
Can anybody help me with this?
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<form runat="server">
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
</form>
I am sharing one idea, but it's not good practice.
#Html.Partial("_ASCX_FILE")
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%# Register Assembly="SomeAssembly" Namespace="SomeNs" TagName="ASCX_FILE" %>
<ASCX_FILE:SomeControl runat="server" ID="fooControl" />
Possibly above solution can sort out your issue.
Suggesting again, try using pure MVC flavor. :)
I have a feeling I'm missing one small thing. I have very simple page, created from the ASP.NET templates in VS2010. My Default.aspx consists of simply the following code. The Site.Master page is doing what it's supposed to.
<%#Page
Title="Home Page"
Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="UserControlTest._Default" %>
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<!-- HERE BE DRAGONS -->
<tsi:BigHelloBanner runat="server" />
<tsi:SmallHelloBanner runat="server" />
</asp:Content>
BigHelloBanner contains this:
<%#Control
Language="C#"
AutoEventWireup="true"
Visible="true"
CodeBehind="BigHelloBanner.ascx.cs"
Inherits="UserControlTest.Controls.BigHelloBanner" %>
<h1>HI!</h1>
Both the codebehind files in both objects are empty, and inherit from UserControl. The behavior is the same inheriting from Control. When I view-source on the rendered output, nothing from the HelloBanners is output, except some newlines. The HERE BE DRAGONS comment is visible, which indicates to me that the master page and all that works fine. I am expecting to see the <h1>HI!</h1> markup in the output as well. What am I missing? This seems really basic.
It looks like that you're referring to the empty code-behind class instead of the ASCX file with the output. Use the src attribute in your #Register directive:
<%#Register
TagPrefix="tsi"
TagName="BigHelloBanner"
Src="BigHelloBanner.ascx" %>
I can't see the src attribute here where is your control held ?
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest"
src="?" %>
Since BigHelloBanner is a web user control, you should try registering it like this:
<%#Register TagPrefix="tsi" TagName="BigHelloBanner" Src="~/pathToUserControls/BigHelloBanner.ascx" %>
Don't you still need to give an ID to each control instance?
I have an issue currently that I can't resolve. I have a user control called "Dashboard" which then has the following markup, containing several subcontrols.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.ascx.cs" Inherits="BlueSEQ.Controls.Dashboard.Dashboard" %>
<%# Register src="Administrator.ascx" tagname="Administrator" tagprefix="uc1" %>
<%# Register src="Provider.ascx" tagname="Provider" tagprefix="uc2" %>
<%# Register src="User.ascx" tagname="User" tagprefix="uc3" %>
<% if (isAdministrator)
{ %>
<uc1:Administrator ID="Administrator1" runat="server" />
<% }
else if (isProvider)
{ %>
<uc2:Provider ID="Provider1" runat="server" />
<% }
else
{ %>
<uc3:User ID="User1" runat="server" />
<% } %>
As you can see, I want it to display some controls or other controls depending on some conditions. However, all of these controls' "Load" event get triggered, even if they are not used.
How can I prevent this?
If you can help it, try to avoid having conditional logic in your markup. It could make the views somewhat more difficult to understand for designers (if you're working with designers) and more difficult to find and refactor this code in the future.
You should also take a look at ASP.NET MVC: Avoiding Tag Soup. Although it's ASP.NET MVC, it's still a good example of how adding logic to your views can quickly make them very difficult and unpleasant to maintain (initial example).
You could use the technique described here: How to: Add Controls to an ASP.NET Web Page Programmatically.
Your markup would look something like this.
<asp:PlaceHolder id="MyPlaceholder" />
and your codebehind would have something along the lines of
private void InitSection()
{
Control c;
if( isAdministrator )
c = Page.LoadControl("~\Administrator.ascx")
else if( isProvider )
c = Page.LoadControl("~\Provider.ascx")
else
c = Page.LoadControl("~\User.ascx");
MyPlaceholder.Controlls.Add(c);
}
The ideal way to do this is to set up asp.net role provider and use a LoginView control, something along the lines of the code below. LoginView only loads the appropriate content.
<asp:LoginView runat="server">
<AnonymousTemplate>
<uc1:User ID="User" runat="server" />
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup Roles="Administrator">
<ContentTemplate>
<uc1:Administrator ID="Administrator1" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="Provider">
<ContentTemplate>
<uc1:Provider ID="Provider" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
You have to Load the control on a specific condition instead, so try to set visible/invisible with the usercontrol, that's a much better approach
<% if (isAdministrator)
{ %>
Page.LoadControl(("~\Administrator1.ascx");
<% }
How about using a MultiView control?
MultiView on MSDN
I had asked a couple of question about multilanguage in asp.net and I am very grateful because the answers have been of much help.
I now face another problem.
I have the page directive:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Galeria.aspx.cs" Inherits="TerapiaFisica.Galeria" %>
What I want is to make the Title multilanguage.
I know i can do that from code behind with something like this:
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = (string)GetLocalResourceObject("PageTitle");
}
But that's exactly what i don't want to do. I want to make that title multilanguage from the tag in the page directive of the aspx.
Anyone can tell me what to do?
I tried this two options but none of them works:
<%# Page Title=" <%= GetGlobalResourceObject("Global", "PageTitle") %>"
and
<%# Page Title="<asp:Localize Text="<%$ Resources: Global, PageTitle %>"
Will this work for you?
<head>
<title><%= GetGlobalResourceObject("Global", "PageTitle") %></title>
</head>
I don't have my IDE in front of me, but the one you wrote (below) looks wrong
<%# Page Title=" <%= GetGlobalResourceObject("Global", "PageTitle") %>"
Did you try
<title>
<%= GetGlobalResourceObject("Global", "PageTitle") %>
<title>
I'm trying to do the following in a aspx page:
<%# Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
<% if (!isOld) %>
<% { %>
<p>display this</p>
<% } %>
</asp:Content>
isOld is a public bool variable from the cs file mentioned in the namespace.
But unfortunately it gave me an unknown error.
I could do something similar in JSPs, but after googling around for a while, I'm not so sure whether the above is achievable in ASP.NET? (Am I missing a tag declaration, or do I have to write the entire tag lib myself?)
Thanks.
EDIT: I just got an unknown error. I have a feeling that the code above has either the wrong syntax or are totally off the wrong track. I tried the following code, and there was no error, but the bool variable is always false:
<% #if !isOld %>
<p> display this</p>
<% #endif %>
in your code front:
<%# Page Language="C#" EnableSessionSTate="true" ValidateRequest="False" Inherits="MyProject.CodeBehind.MYWF.SiteWF" MasterPageFile="~/_layouts/application.master" %>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
<asp:PlaceHolder runat="server" id="PlaceHolderIsOld">
<p>display this</p>
</asp:PlaceHolder>
</asp:Content>
and then in your code behind:
protected void Page_Load(object sender, EventArgs e){
PlaceHolderIsOld.Visible = IsOld;
}