How to access data binding object in aspx page in ASP.NET?
I am trying to hide or show a certain section of my table depending on the value of a property in my binding object(s).
public class Class1
{
public bool Display { get; set; }
}
In ASP.NET MVC, I can just do the following (assuming that Class1 is the model that binds to the page.)
<table>
<tr>Row 1</tr>
<tr>Row 2</tr>
<% if(Model.Display) { %开发者_Go百科>
<tr>Row 3</tr>
<tr>Row 4</tr>
<% } %>
</table>
How can I achieve the same behavior in transitional ASP.NET? That "Model" variable is not available. How do I retrieve the data binding object? Thanks.
Add a property to your page class that has the information you need. You can then reference it from the markup.
Codebehind:
class PageClass : System.Web.UI.Page
{
protected Class1 SomeImportantInfo { get; private set; }
}
Markup:
<table>
<tr>Row 1</tr>
<tr>Row 2</tr>
<% if (this.SomeImportantInfo.Display) { %>
<tr>Row 3</tr>
<tr>Row 4</tr>
<% } %>
</table>
During data binding you can subscribe to events such as OnRowDataBound, etc. These often have an e.DataItem property on the EventArgs. With this you can cast the e.DataItem to the type of object in the collection bound.
精彩评论