Reputation: 15071
I'm new to ASP.NET, but I've seen a lot of tutorials and even have a book that shows how to do really cool stuff with ASP.NET Server Controls and the general impression I get is that it is the 'right' way to do things in ASP.NET.
I've found examples that show me how to generate raw HTML too - it reminds me very much of ASP. But is it the equivalent of using/misuing 'GoTo' statements; or is it acceptable?
In my situation I have a bunch of 'containers DIVs' that each hold a handful of images. Think of a YouTube-type page where each group of videos is of a particular type. This content will be dynamic and pulled from a database. Everything inside of these containers will be nothing more than pretty, formatted, links to other parts of the site; there will never be any server-side manipulation....but I've been given a sample of the exact formatting they want for each container and each item in the container, already in HTML. My first instinct was to just use that HTML exactly, with the appropriate data.
But I don't know if this is an appropriate solution. It works in that, on my dev machine, it renders the page exactly how I want...but there are plenty of bad-practices that would meet that requirement.
Upvotes: 2
Views: 117
Reputation: 25339
If you have content in a database and each content item has the same basic mark-up then you should be looking at using a server control that uses templates to render your content - such as a Repeater or a ListView. Generally this gives you much more readable code with a clear(er) separation of layout and logic. Looping around and concatenating strings to build up HTML is really not the ASP.NET way, as I think you suspect, as you wouldn't be asking the question.
Ideally you would want to return your content from your database as collection of objects (such as IList<T>()
where T is a custom object that holds your database content for each row). You can then bind your list to the Repeater or ListView and use a template to define the markup. You could also create a User Control that has properties that match your mark-up and use this, if you think you may be re-using this approach in other places in your website.
Upvotes: 2
Reputation: 6277
We work with dedicated HTML/css developers. Any HTML generated from the server side codes causes big problems for them. They have to get right into the C# to amend and they hate it.
For my money - I would keep HTML out of C# code and use usercontrols as mentioned before. It better fits the architecture of ASP.Net anyway
Upvotes: 1
Reputation: 1014
There are other way also that you can deal with it.
If you are getting some collection of data for those 'container DIVs', you can generate the HTML by using inline coding in the page. Inline code will allow you to keep the code separate from the code behind logic and maintainability of the code will also be easier. Otherwise in code behind you'll be having a lot of string joining to create HTML and then render it.
The other way to handle the stuff with javascript but I guess you are not thinking on that direction.
Upvotes: 1
Reputation: 23561
In your case you should create a user control to represent one item and use a databound control (most probably ListView) as a template. You can achieve whatever HTML you like this way. You can customize the item template, you can customize the control that represents one item, you can use alternating templates if you want a pattern and you can use a group template to create table-like experience. While generating HTML directly can be useful in certain occasions this is definitely not one of them.
Upvotes: 2
Reputation: 424
yes, we can crate/write dynamic html in ASP.NET, there is absolutely no problem. Also we can run the HTML Page in ASP.NET application.
Upvotes: 0