如果在aspx页面中声明

Bra*_*rad 89 asp.net if-statement

我想在我的网站上写一个基本的if语句来显示项目1或项目2,具体取决于变量是否设置为true.

我不太熟悉.NET,需要一些帮助来获得如何在aspx页面上使用if语句的基本结构

Kri*_*ast 174

如果目的是显示或隐藏页面的一部分,那么您可以执行以下操作

1)用标记包装它

<% if(somecondition) { %>
   some html
<% } %>
Run Code Online (Sandbox Code Playgroud)

2)将部件包装在Panel控件中并在代码隐藏中使用if语句来设置Panel的Visible属性.

格兹,克里斯.


عثم*_*غني 39

只需使用简单的代码

<%
if(condition)
{%>

html code

<% } 
else 
{
%>
html code
<% } %>
Run Code Online (Sandbox Code Playgroud)


Dan*_*olo 9

通常情况下你只是坚持的代码Page_Load在你的.aspx页面的代码隐藏.

if (someVar) {
    Item1.Visible = true;
    Item2.Visible = false;
} else {
    Item1.Visible = false;
    Item2.Visible = true;
}
Run Code Online (Sandbox Code Playgroud)

这假设你已经在页面上得到Item1Item2布局了.


Мар*_* П. 7

使用母版页的 VB.NET aspx 页面标题中可选内容的完整答案:

 <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="some_vb_page.aspx.vb" Inherits="some_vb_page" %> 
 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">          
     <% If Request.QueryString("id_query_param") = 123 Then 'Add some VB comment here, 
         'which will not be visible in the rendered source code of the aspx page later %>        
         <!-- add some html content depending on -->
         <!-- the condition in the if statement: -->                
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript" charset="utf-8"></script>
     <% End If %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

您当前页面的网址类似于:

http://mywebpage.com/some_vb_page.aspx?id_query_param=123