1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

choosing a control to display premade html+data

Discussion in 'C#' started by nubsii, May 16, 2008.

  1. #1
    I programatically generate some fair sized html tables in one of my web apps. These tables are constructed by a stringbuilder and returned as a string. I like this model because its modular. Is there a common practice for displaying such items? I've been using a Label, and setting the Label.Text equal to the string that I create.

    Whats a better way? (note: i want to meld my html+data in a layer away from the page - I don't want to use gridViews and the like)
     
    nubsii, May 16, 2008 IP
  2. fhirzall

    fhirzall Guest

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use a Repeater. That will put your html in the ASPX page and the data in your code behind. It lets you control the HTML and doesn't spit out its own.

    http://www.sitepoint.com/article/asp-net-repeater-control

    If you're using .net 3.5, there's the new Listview control which is like a hybrid gridview-repeater control, this gives you paging and gridview features but lets you completely control the HTML.

    http://weblogs.asp.net/scottgu/arch...a-product-listing-page-with-clean-css-ui.aspx

    I'm not sure what you can do if you're wanting to completely stay away from the controls, you can write your own usercontrol that renders your html, I don't see why though since the repeater kinda does it for you.

    Edit: Another way you could do this is write a class that writes out your html [if it's all tables], pass in your data object and loop through it returning a string of HTML, messy but i suppose it works.
     
    fhirzall, May 16, 2008 IP
  3. nubsii

    nubsii Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I read about the repeater, and it looks promising. I think I'll use one to implement a user control panel in the near future. What I didn't understand was: can I programatically alter the number of data items that I return with a repeater?

    Here's some more details about my app:
    Your "edit" comment is pretty close to what I do. I'm using asp.net 2.0 and I wrote some pagination code/ui that structures SQL queries based on the desired range of data. I ended up with a "class that writes out my html" because I wanted to copy the functionality of MySQL LIMIT 10,10 as it applies to pagination. Most specifically I wanted to grab small chunks of data and then not risk having duplicate content on the last page. For example, if I have 46 items of data and each paginated page shows 10 items, then the last page should show the last 6 items of data. In mysql the same query that generates the first 4 pages of 10 items will also generate the last page of 6 items. In mssql I need two different queries to do this, else the last page will have 10 items. I'm the only C#/MSSQL dev on a team of PHP/MySQL devs and I'm forced to make very similar looking products.

    I'm gathering that I'm probably am stuck with using a class to write out the HTML. Hopefully I haven't over engineered this whole thing just to compete with my PHP counterpart.

    Here's the code that selects a limited number of items - it doesn't actually do what MySQL LIMIT does until you account for how I control the last page.
    
        public DataTable selectLimit(int numberOfItems, int lastItemToSelect)
        {
            DataTable ret = new DataTable();
            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = sqlConn.CreateCommand();
                cmd.CommandText =
                    "select * from (" +
                     "select top " + numberOfItems.ToString() + " * from (" +
                        "select top " + lastItemToSelect.ToString() + " *" +
                        "from mytable " +
                       "order by date asc" +
                     ") as LOLatMS order by date desc" +
                    ") as LMAOatMS order by date asc";
                sqlConn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
    
                ret.Load(sdr);
                return ret;
            }
        }
    
    Code (markup):
    The piece of the logic that requests the last page. Note: the pageRequested variable is the value of the &page= query from the url.
    
    //Creates the last page and hides next/last links
                else if (pageRequested * itemsPerPage > totalItems)
                {
                    DataTable data = DAL.selectLimit(totalItems % itemsPerPage, totalItems);
                    TableView tv = new TableView();
                    Label1.Text = tv.createTable(data); 
                    HyperLinkFirst.NavigateUrl = FirstPageUrl();
                    HyperLinkPrevious.NavigateUrl = PreviousPageUrl();
                }
    
    Code (markup):
    And finally, the very unexciting bit of code that actually makes the table in the test version of our webapp (at least its modular)
    
    //belongs to TableView class
        public string createTable(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=\"1\">\n");
            foreach (DataRow record in dt.Rows)
            {
                sb.Append("<tr>\n<td>" + record["stuff_col_name"] + "</td>\n" +
                    "<td>" + record["col_name"] + "</td>\n" +
                    "<td>" + record["col_name"] + "</td>\n" +
                    "<td>" + record["date_col_name"] + "</td>\n" +
                    "</tr>\n");
            }
            sb.Append("</table>\n");
            return sb.ToString();
        }
    
    Code (markup):
     
    nubsii, May 16, 2008 IP
  4. fhirzall

    fhirzall Guest

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi nubsii,

    A repeater will work for what you're doing. Take a look at this example:
    http://aspnet.4guysfromrolla.com/articles/081804-1.aspx

    It basically has a repeater with paging, there's a class in .net called the PagedDataSource which controls the limit/amount of records returned. When you use the pagedatasource, all the records are returned and then paged - if you want to do paging on the database level [only return the # of rows needed], you can keep that function you have and for your next page/previous page button load the needed records.

    I see that you're query just concatenates the variables there, I suggest using parameters to protect from SQL injection.
    http://aspnet101.com/aspnet101/tutorials.aspx?id=1

    I haven't encountered a situation where the repeater didn't work for me. In essence what you're doing is writing the functionality behind the repeater, so I'd try it out if I were you. If you run into any problems, just google for 'asp.ne repeater paging' and you should find a ton of articles.
     
    fhirzall, May 16, 2008 IP
    nubsii likes this.
  5. nubsii

    nubsii Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can I use parameters in this query??

    
        public DataTable selectLimit(int numberOfItems, int lastItemToSelect)
        {
            DataTable ret = new DataTable();
            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = sqlConn.CreateCommand();
                cmd.CommandText =
                    "select * from (" +
                     "select top " + numberOfItems.ToString() + " * from (" +
                        "select top " + lastItemToSelect.ToString() + " *" +
                        "from mytable " +
                       "order by date asc" +
                     ") as LOLatMS order by date desc" +
                    ") as LMAOatMS order by date asc";
                sqlConn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
    
                ret.Load(sdr);
                return ret;
            }
        }
    
    Code (markup):
    When I wrote that query in SQL Management Studio I attempted declaring things like "@numberOfItems" and "@lastItemToSelect" using declare/set commands. It threw an error so I assumed that @vars could only be used in place of data values, such as dt_processed = @date. Am I wrong about this? I'd like to be wrong about this. :)

    As it stands now the 'text' that is concatenated to the query is casted to int32 in the business layer of the app, hopefully that is protecting me from injection for now. (?)
     
    nubsii, May 19, 2008 IP
  6. MatthewDP

    MatthewDP Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    You can also just generate a bunch of HTML and shove it into a label or a literal :)
     
    MatthewDP, May 26, 2008 IP