简体   繁体   English

如何在ASP.NET的aspx页面中访问数据绑定对象?

[英]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.) 在ASP.NET MVC中,我可以执行以下操作(假设Class1是绑定到页面的模型。)

<table>
    <tr>Row 1</tr>
    <tr>Row 2</tr>
    <% if(Model.Display) { %>
    <tr>Row 3</tr>
    <tr>Row 4</tr>
    <% } %>
</table>

How can I achieve the same behavior in transitional ASP.NET? 如何在过渡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. 在数据绑定期间,您可以订阅诸如OnRowDataBound等事件。这些事件通常在EventArgs上具有e.DataItem属性。 With this you can cast the e.DataItem to the type of object in the collection bound. 使用此方法,您可以将e.DataItem强制转换为集合边界中的对象类型。

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

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