PHP vs ASP.NET Part 3

Custom tables with hidden values

And so the series continues. This time with what is probably a more substantial problem than in the first or second parts of this set. In summary, what I've learned so far from the feedback is that ASP.NET resources are sadly lacking by comparison to PHP, and as a result the "simple" workarounds in ASP.NET are largely hidden from the novice programmer. This is the only avenue through which I've been able to get any help so I'm once again appealing to the greater knowledge of the Stardock community to save me from the darkness of ignorance! :)

The Problem:
I have a table of items that I want to display based on variables passed to the page from a previous form. Here's a quick example of a table for purposes of discussion...

Section Heading    
  Category Heading  
    Item 1
    Item 2
    Item 3
  Category Heading  
    Item 1
    Item 2
Section Heading    
  ....  

I want to be able to hide/reveal any given item (and all items underneath it if necessary) if certain conditions aren't met. For example lets say Item 1 under the first Category Heading needs to be hidden. Items 2 and 3 should still display. Also, if Section Heading 2 is invisible, everything underneath it should be invisible too.

The PHP Way
If I was doing this in php, I'd have the code with the IF statements nesteled within the table itself. This would be a bit messy to maintain, but it would work. The other way I could do it is construct the entire table within the PHP code somewhere else, and then just display the variable on the page. Either way, it would be pretty simple but difficult to maintain in the long-run. I could clean it up by giving each item a variable name and then construct the larger string by piecing together variables.

The ASP.NET Way
I don't know how to properly do this. As it stands now I'm constructing ALL of my page HTML code within the C# script header and then just outputting the resultant string <%=StringVar%> within the body area. Like I said above, this is a very messy way to do it. I know there are ways to use the ASP.NET Table controls to have the entire table with all possible results setup and then just toggle which items and rows you want to make visible or not. However all of my searching and reading has kept pointing me towards manipulating table data from a database, or just how to construct an ASP.NET table, but not how to on the fly toggle content visible or invisible.

Is there a simple way to do this? I would much rather have the table built and then just toggle rows on and off as I need to, it would make everything so much more maintainable.
11,387 views 4 replies
Reply #1 Top
You could do the ASP.NET version very much the same way as you would with the PHP version by just using ASP.NET statements inline with your HTML. If this table is to be used on more than a single page, you should then consider moving the table HTML and logic into a "User Control" which can then be included on any page. User Controls can have properties and methods defined, just like any class, and if implemented could give you programmatic control over the rendering of the HTML.

The part of your article that concerns me is that you say you are building all of the HTML within a string in the header and then displaying it. That sounds like a horribly difficult way of doing it, no wonder you are having such a hard time with ASP.NET! While something like that may work, you are not taking advantage of any of ASP.NET's real features. You should probably take a step back, learn a bit more about the capabilities of ASP.NET and think about a structure that isn't so limited.

I'd start off by going here and learning more about User Controls and Data Binding, which is the technique I would suggest for your table situation. Keep in mind that data binding isn't limited to recordsets or database information, you can data bind to nearly any class property, enumerable or not.
http://www.asp.net/Tutorials/quickstart.aspx

I would also suggest you look at the source code of the sample sites they have, this should give you a better idea of "best practices" to use when programming sites with ASP.NET. http://www.asp.net/Default.aspx?tabindex=8&tabid=47

Good luck!
Reply #2 Top
I understood that building it all in strings in the code was the wrong way to do it, unfortunately I wasn't able to find any examples or hints on how to do what I wanted to do, so I went the way I knew already to save some time.

I've been trying to find a few solid examples of the ASP.NET table controls, but I haven't been able to find anything on setting specific rows visible/invisible yet.

Thanks for your help!
Reply #3 Top
Off the top of my head, you can make a class that controls that table filled with data, like hide/show, styles etc.. It would take a few hours to build, although it would be worth it if the table would become large.

I think if you set the Visible attributes to off/on it removes that cell it self. In place of that you would use the InnerHtml = ""

And the data can be anything, xml,text,db or manual feeds. You can simply build a dataset with that data.
Reply #4 Top
Hello zoomba, ASP.NET is not my web language of choice (Perl is) but dont be too hard on it. I agree it might be more complicated for a newcomer but you will not be a newcomer forever Most difficulty in programming is not caused by the language itself but knowing how to approach problem solving from a programming perspective. IDEs or even languages do not make a good programmer.

If a newcomer had a choice I would suggest they also start with perl as it is easy to start with and then they can turn on strictures and code more professionally later on.