<< Click to Display Table of Contents >> Programação inline (dentro do aspx) |
![]() ![]() ![]() |
É possível criar alguns códigos dentro do aspx.
Tipos de inline
Tag |
Descrição |
Exemplo |
Link |
<% ... %> |
Roda código normal |
<% if (User.IsInRole("admin")) { %> You can see this <% } else { %> You are no admin fool! <% } %> |
http://msdn2.microsoft.com/en-us/library/ms178135(vs.80).aspx |
<%= ... %> |
Retorna um valor de variável ou função. Semelhando a Response.Write |
The Date is now <%= DateTime.Now.ToShortDateString() %> |
http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx |
<%# .. %> |
Usando para expressões binding. Como Eval ou Bind. Encontrados em GridView, Repeater, etc |
<asp:Repeater ID="rptMeetings" DataSourceID="meetings" runat="server"> <ItemTemplate> <%# Eval("MeetingName") %> </ItemTemplate> </asp:Repeater> |
|
<%$ ... %> |
Usando para expressões, não para código. Use com DataSources |
<asp:SqlDataSource ID="party" runat="server" ConnectionString="<%$ ConnectionStrings:letsParty %>" SelectCommand="SELECT * FROM [table]" /> |
|
<%@ ... %> |
Diretivas. Geralmente vista no início da página aspx |
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <%@ Register TagPrefix="wp" Namespace="CustomWebParts" %> |
http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx |
<%-- ... --%> |
Comentário |
<asp:Label ID="lblAwesome" runat="server" /> <%-- sometimes end users make me angry --%> <asp:LinkButton ID="lbEdit" Text="Edit" OnClick="Edit_Click" runat="server" /> |
Exemplos
Nestes exemplos abaixo o CSS define um div chamado #tudo para 1400px quando a variável de sessão chamada adm for true (bloco azul). Se for false, o #tudo será 1120px (bloco verde):
Exemplo 1 com <% ... %>
<head runat="server">
<title>Tarefas Market</title>
<link href="visual/estilos.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<%
bool tela_grande = false;
if (Session["adm"] != null)
tela_grande = (bool)Session["adm"];
if (tela_grande)
{
%>
<style type="text/css">
#tudo
{
width: 1400px;
margin: 0 auto;
text-align: left;
float: left;
border: 1px solid red;
}
</style>
<%
}
else
{
%>
<style type="text/css">
#tudo
{
width: 1120px;
margin: 0 auto;
text-align: left;
float: left;
border: 1px solid red;
}
</style>
<%
}
%>
</head>
Exemplo 2 com <%= ... %>
Default.aspx
<head runat="server">
<style type="text/css">
#tudo
{
width: <%= GetTamanho() %>;
margin: 0 auto;
text-align: left;
float: left;
xborder: 1px solid red;
}
</style>
</head>
Default.aspx.cs
public string GetTamanho()
{
string retorno = "1120px";
if (Session["adm"] != null)
if ((bool)Session["adm"])
retorno = "1400px";
return retorno;
}
import in-line
<%@ Page Title="" Language="C#" MasterPageFile="~/Mestre.Master" AutoEventWireup="true"
CodeBehind="Cadastro.aspx.cs" Inherits="Tarefas.Cadastro" %>
<%@ Import Namespace="Tarefas.classes" %>
Veja mais em