简体   繁体   English

检查aspx页面中的对象属性

[英]check object property in aspx page

I have a property public Client Clients { get; set; } 我有一个属性public Client Clients { get; set; } public Client Clients { get; set; }

If I have an object on load as so: 如果我有一个加载对象,如下:

Client objClients = populate();
if (objClients != null)
{
    Clients = objClients;
}

Would I be able to access the properties of this object in aspx page eg in if statement. 我是否能够在aspx页面中访问此对象的属性,例如在if语句中。

I have done like following but my page comes blank and the load event don't run so I assume it is not correct: 我做了类似跟随,但我的页面空白,加载事件没有运行所以我认为它是不正确的:

<%if (this.Clients.Address1.Trim().Length > 0)
 { }%>

EDIT:::: 编辑::::

if i do this 如果我这样做

public string Address1 { get; set; }
Client objClients = populate();
if (objClients != null)
            {
 Address1 = objClients.Address1;
}

and then in aspx file do this it works fine any reasons??? 然后在aspx文件中执行此操作它可以正常工作???

 <%if (Address1.Trim().Length > 0)
                      {%>
                      <%= Address1 %><br />
                    <%} %>

You can adjust your { } in order to to make difference between <% and <%#, maybe you want inject datas in your {}, and for this need you must use <%# 您可以调整{}以便在<%和<%#之间进行区分,也许您希望在{}中注入数据,并且为此需要您必须使用<%#

<%= is for injecting values, <%=用于注入值,

<% is used to run code. <%用于运行代码。

For this code i inderstand but without { }, if { } contains code inside who inject datas you must use <%#. 对于这个代码我理解但没有{},如果{}包含注入数据的代码,你必须使用<%#。

<% if (this.Clients.Address1.Trim().Length > 0) %>

You still haven't posted the whole of your code behind/aspx.cs file as I think you might have an error in it. 你仍然没有在/ aspx.cs文件后面发布你的全部代码,因为我认为你可能有错误。 But I got this working with no problems at all. 但我完全没有问题。

Code-Behind 代码隐藏

namespace WebApplication1
{
    using System;

    public partial class _Default : System.Web.UI.Page
    { 
        public Client Clients { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Client objClients = populate();
            if (objClients != null)
            {
                Clients = objClients;
            }
        }

        private Client populate()
        {
            return new Client() { Address1 =  "Somewhere in London" };
        }
    }

    public class Client
    {
        public string Address1 { get; set; }
    }
}

Mark-up 加价

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <% if (Clients.Address1.Trim().Length > 0){ %> 
            <%= Clients.Address1 %><br /> 
        <% }%>
    </form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM