Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to access properties of my code behind class from the markup in ASP.NET

Tags:

scope

c#

asp.net

Let's say I have two files default.aspx and the associated default.aspx.cs.

default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    var myObject = new MyObject();
}

Is there a way that in the default.aspx I could do something like:

<%= myObject.SomePropertyOfThisObject%>

... or something similar, without having to use databinders or something complicated like this? And if there's no way around binding the data, what would be the best way to do it?

like image 806
marcgg Avatar asked Jul 06 '09 21:07

marcgg


People also ask

How can I see the code behind ASPX?

Right-click the . aspx page, and then click View Code. The code-behind file opens in the editor.

What are the advantages of using code behind?

The advantages brought by the code-behind model are clear and to a large extent also unquestionable. You deal with classes and integrate your own classes with the existing framework. The code and layout physical separation enable graphic designers and page developers to work on the same resource at the same time.


2 Answers

You can, but you'll have to do it a bit differently. In your default.aspx.cs, add a member:

protected MyObject _myObject;

Then, in Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
         _myObject = new MyObject();
}

Then, in default.aspx, you can do:

<%= _myObject.SomePropertyOfThisObject %>

Of course, this assumes that class MyObject has a property named Awesome. You didn't mean the System.Object class, did you, since it doesn't have a property named Awesome.


Since your question was asking about the best way, I'll go further. The way I showed is not the best. The best is more often to use a data binding expression. If you don't like those, then you can set things in the codebehind:

protected void Page_Load(object sender, EventArgs e)
{
         _myObject = new MyObject();
        //
        _myLabel.Text = _myObject.SomePropertyOfThisObject;
}

Assuming:

<asp:Label runat="server" id="_myLabel" />
like image 73
John Saunders Avatar answered Sep 22 '22 22:09

John Saunders


The <%= ... %> is ASPX short-hand for Response.Write( .... ).

If you change myObject to be strongly types (rather than just types as Object) you can certain use the line:

<%= myObject.Awesome %>

to emit a value.

Be aware, however, that the <%= syntax has limitations - specifically:

  • it cannot be used within HTML attribute as a value
  • it cannot emit server-side elements
  • it cannot be used within ASP UpdatePanels - you will get a exception when you perform a partial postback

You are probably better off creating a Label control on you page, and programmatically setting the Text property to the value. This also gives you more control over how value-to-string conversions are performed. And will work correctly with update panel controls.

like image 40
LBushkin Avatar answered Sep 22 '22 22:09

LBushkin