Is good or bad to write everything in c# rather than using web controls?

219 Views Asked by At

I'm confused with choosing between several ways of getting result in ASP.NET.

For example, Web form control SqlDataSource, you retrieve data from database and show results in other controls such as DataGridView, BulletedList etc. However all those things can be written in C#, creating a string which will hold your HTML codes with the retrived data, then you insert your Html code into div using innerHTML. What's the difference?

Example:

[ <div id='block1' runnat='server'></div]  

and in CodeBehind

[ block1.innerHTML = myString;]

After writing C# code SqlConnect, Loops, Datatable, you put value of your HTML string into myString.

Why not to implement everything with C#?

3

There are 3 best solutions below

0
On

Think about what's easiest. For simple cases, using markup, templates and databinding is usually easiest and most simple, because most of what's written is static markup - so we can stay in markup's "native land". But if the markup could radically change based on programmatic logic, then trying to express that in ASP.NET markup can be tedious at best.

Also think about deployment and reuse - templates might also be easier to maintain for a single application, but harder to package and reuse in different applications.

You want to minimize effort and complexity. Achieving these flow directly into less bugs and more stability, plus shorter delivery time. So think about how effort and complexity are affected by:

  • How hard will it be for you to write?
  • How hard will it be for you (or others) to change? - if this is a throwaway application, or unlikely to change much, this is less of a concern.
  • How hard will it be to deploy?
  • How hard will it be to reuse? - if there is no reuse, this is not a concern.
0
On

Writing it all in pure C# is possible but not very convenient when you are trying to achieve a specific html layout - it is painful to maintain, and very hard to work alongside a developer if you want to take their html and just tweak it to add the data.

Personally I'd look at MVC here; for example, I've been playing with razor recently which allows very elegant integration between C# and html in the same file:

<div id="@obj.Id">
  <ul>
    @foreach(var item in obj.Items) {
      <li>@item.Name</li>
    }
  </ul>
</div>

There I can:

  • clearly see at a glance how the code maps to the source I can see at the client
  • make changes with confidence, both from visual inspection and the IDE telling me if I do something obviously wrong
  • compare to the designer's draft easily
0
On

Mostly for maintenance reasons.

Can you imaging how much difficult it can get to make changes to it or debug it? And since it is not a traditional approach, any programmer after you that has to work on that code will of course not be happy with it.

Always remember,

  • HTML is for markup (for example, Building)
  • UI customization/styles go to CSS and Themes are for Server Control customization (for example, Paint)
  • C# (or code-behind to be specific) is for logic (for example, Amenities or wiring up).