SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Outputting an object to the page with something besides databind

    Hi guys,

    I am fairly new to asp.net and just had a quick question on the best practice for getting an object(namely properties) outputted to the page using something other than databind. Is this even possible?

    Say I would like to use datatables.net to handle the behavior of a collection of objects client side in a table format. In PHP this was a simple process. Instantiate and populate your object from the database, fill an array(collection whatever) with your objects, and then simply output in your view like so:
    PHP Code:
      <table>
        <thead>
            <th>obj prop header</th>
            <th>etc.</th>
        </thead>
        <tbody>
    <?php foreach (objCollection as obj)
                    {
            
    ?>
             <tr>
               <td><?php echo $obj->name?></td>
               <td><?php echo $obj->address?></td>
             </tr>
             <?php 
                     
    }
             
    ?>
        </tbody>
      </table>
    Is there any way to do something similar to this in asp.net without using databinding? Or would you have to databind the object to a server control(say the gridview or whatever) and specifiy each column and use <%#eval("prop name")%>?

    Everywhere I read, they want me to use some damn wysiwyg tool to drag & drop stuff on the page. They almost want all your app layers to bleed into the same place.

    By the way this is a webforms site unfortunately Any thoughts or suggestions. Even if you could point me in the right direction for some docs, that would be great.

    Thanks Sitepoint!
    Last edited by xxjhansonxx; Nov 10, 2011 at 15:48. Reason: code mistake

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You probably want to check out ASP.NET MVC, it is much better for this sort of thing and pretty much works the way you are talking about.

    In any case still doable in ASP.NET Web Forms. Trick(s) are:

    1) Object needs to be a protected member of your page class, public works too. Remember that the rendered page is a generated class descneding from the class you define.
    2) From there, you can either use databind or response.write. The former is probably cleaner for the pipeline, but either works if you don't have too many complex interactions on the page. To databind things, use

    <%# MyVar.MyProperty %>
    Note you will have to call DataBind() on the container somewhere.

    For the response.write thing, you could do:

    <%= MyVar.MyProperty %>
    You can also inline code to get to your example:

    <table>
    //header
    <tbody>
    <% foreach (var x in MyCollection) { %>
    <tr>
    <td><%= x.Field1 %></td>
    <td><%= x.Field2 %></td>
    </tr>
    <% } %>
    </tbody>
    </table>
    But that is really, really unidiomatic in .NET webforms. You might want to look at the repeater which handles the looping construct, naming container aspects without generating a shred of markup.

  3. #3
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    May I ask why the aversion to databinding?

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks wwb_99!

    I think the repeater will help in these cases. It looks like databind is the only way to go, but at least there is a tag that helps me limit some of the automagically generated markup. The only problem that I had with some of the other tools is that it generates too much extra stuff for me and then I have to look into firebug to see what it actually generated. I like to put together my own markup for the most part and be able to control the ids etc.

    I am still unskilled in .NET land as far as asp tags go so there very well could be a better way, but it seems like a lot of property configuring to get the desired effect and I like to avoid inline styles all together. The .NET class library is awesome though, don't get me wrong.

    I haven't looked into asp.nets MVC yet, but I have heard good things and that is more the background that I came from in php. Unfortunately the site I have to maintain is all web forms that was done procedurally so it's spaghetti dinner for me but any project going forward, that will definitely be my cup of tea.

    Does Sitepoint have any Books on MVC in asp.net in the mix?

    May I ask why the aversion to databinding?
    ImagineKitty -

    It's not that I am totally against it, it's just that the examples that I have read want you to use databinding with gridviews, etc, and use all of these different properties, commands for updating, deleting, etc. I just like to keep my layers(presentation, behavior, business logic, data access) all seperate.

    When I see what all of these nifty tools spit out to the page, it just makes me cringe a little. Maybe it's a control issue but I like control.

    I'll just have to keep plugging away with the best practices and there is no better spot to get them than Sitepoint! You Da Best.

    Thanks!

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Look into ListView in addition to Repeater. Before I went to MVC I really liked it.

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will do imaginekitty, a big thanks for the tip!

    P.S. I am scared as hell of clowns as well......brrrrrr. I can just picture Stephen Kings "IT".

  7. #7
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Make sure to check for dates on .NET tutorials. There is so much outdated garbage in the .NET world. Even here at sitepoint.

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I feel ya on the spaghetti angle. That said, you can do some pretty slick stuff with MVVM and webforms if you build things right. But you are so better off going with MVC these days it isn't even funny.

  9. #9
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    ... But you are so better off going with MVC these days it isn't even funny.
    I definitely agree. Unless you've been hired to maintain something already written using Web forms then you may as well jump into MVC.

  10. #10
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    I feel ya on the spaghetti angle. That said, you can do some pretty slick stuff with MVVM and webforms if you build things right. But you are so better off going with MVC these days it isn't even funny.
    What's MVVM? I don't think I will be able to convert this site over for a couple years to MVC as it has a very large backend that is integrated into internal applications, but I could make what I do touch better. They know they need to take a different direction so it will eventually come. Slowly eat at the elephant chuck by chunk.

    We have a sub-division e-commerce site that they want re-done. I am excited as this will be my chance to get into .NET MVC and actually make some re-usable, scalable code. Do you guys have any good resources?

    Thanks again!

  11. #11
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    MVVM is Model - View - ViewModel pattern. http://en.wikipedia.org/wiki/Model_View_ViewModel

    You can do Inversion of Control and Dependency Injection in Web Forms but by the time you have that sorted you could have just learned MVC.

  12. #12
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    MVVM is Model-View-ViewModel. Really mainly applies to silverlight or WPF work, but you can borrow alot of concepts for webforms work. The crux of it is you make your back-end feed a presentation layer ViewModel object which is the only thing the page it self interacts with. Basically you can't avoid having some messy spaghetti code without undue effort, but it isn't too hard to keep that mess confined to the tip of the UI layer. Coincidentally this also opens the door to upgrading the UI layer to something cleaner . . .

  13. #13
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    If you want to make ASP.NET act more like PHP, I can show you how. Just keep reading. One thing to keep it mind, ASP.NET is not PHP, and there will be some things you can't do, and other things that you will do a little differently.

    1) Create a new "ASP.NET Empty Web Application" and name it "ASP.PHP".

    2) Adjust the Web.Config so that it has the following content:

    Code XML:
    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <pages>
          <namespaces>
     
          </namespaces>
        </pages>
      </system.web>
    </configuration>

    3) Add any namespaces you desire your pages to include in the space provided, like so:


    Code XML:
    <namespaces>
      <add namespace="System.Linq"/>
    </namespaces>

    4) Add a new "Text File" to the solution and name it "Default.aspx". You will note that by doing so, you only get one file, and do not get the code-behind or designer files. This is expected.

    5) Add the following line at the top of the page. It must be the first line!

    Code ASP:
    <%@ Page Language="C#" %>

    6) You can now write code just like you would in PHP, in a linear fashion. You can still make use of custom namespaces and classes within your app. For example:

    Code ASP:
    <%@ Page Language="C#" %>
    <!-- pre-render-processing-->
    <%
        // set up page vars here
        string title = "My Page Title";
        string greeting = "Hello World!";
        // handle postback if needed
        if (IsPostBack)
        {
            greeting = Request["greeting"];
     
        }
        // get a list of data (sample)
        IList<string> values = new List<string>();
        values.Add("Foo");
        values.Add("Bar");
        values.Add("Baz");
    %>
    <html>
    <head>
    <title><%=title%></title>
    </head>
    <body>
    <h2><%=greeting%></h2>
    <ul>
    <%foreach (var value in values){%>
    <li><%=value%></li>
    <%}%>
    </ul>
    </body>
    </html>

    7) You can use standard SSI syntax to include other similarly created pages into your content. You can include any System or Customer namespace via web.config, and use any classes there-in. Have fun.

  14. #14
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Or you could just use ASP.NET web pages and get to the same place but in a whole lot cleaner manner.

    SSIs are a technology best left in the scrap heap of the 90s.

  15. #15
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I agree with you wwb, 1000%, but the original poster stated his app is already webforms. He wanted to get away from the drag and drop, and wanted to move towards something more linear, even providing a PHP code snippet as an example. I was merely instructing him on how he can achieve what he asked about.

  16. #16
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for all the feedback! I will have to bear with the webforms for now, but at least I can minimize the markup a little.

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •