0

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

2
  • What are you databinding to? Is there a GridView, FormView or something else on this page? Commented Dec 28, 2010 at 22:09
  • I am binding a collection of objects to a Telerik RadGrid control and the table resides in the nested template of each binding object row. Commented Dec 28, 2010 at 22:31

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

My original intention was to use the data binding object directly in the aspx page hoping to somehow leverage DataBinder. I am still curious whether there is any way to go through that route. But this would be a solution too. Thanks.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.