Zaki
Zaki

Reputation: 5600

check object property in aspx page

I have a property 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 e.g. in if statement.

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???

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

Upvotes: 0

Views: 717

Answers (2)

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

You still haven't posted the whole of your code behind/aspx.cs file as I think you might have an error in it. 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>

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

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) %>

Upvotes: 1

Related Questions