Usercontrol performance when using it for generating content - c#

Well, I know there is lots of questions about usercontrol performance, my question is perhaps different.
I would like to generate my webpage dynamically and unfortunately creating a code for that is a pain when the code contains lots of DIVs, IMGs and other controls.
Usercontrols has a advantage becuase they are created by markup code and It's easy to add getters/setters. That means I can easily edit my code and also control the content.
These usercontrols only contain HTML code with few controls like Label or Textbox. That's all.
I would like to ask, if my approach is ok. What's your opinion about that ?
Thank you

Here is a great article describing some pros and cons on using WebForms versus something like MVC. I've used both and like MVC #tonsupontons better.
http://www.codeproject.com/Articles/528117/WebForms-vs-MVC
Your question is somewhat of a religious debate however so I don't think there's a good answer. If it works and isn't bloated full of view state nonsense and you don't do things like Unit Testing then WebForms and UserControls work fine and you should not really notice a performance hit one way or another. <= My opinion I think though, with some exposure to MVC, you might find it much easier to do what you're doing

Related

ASP Labels? Why not just =Variable?

In Classic ASP, I loved the fact that I could write out variables, formulas, etc., etc. with a simple:
<span class='<%=myClass%>'><%=myariable%></span>
I could format it, or do anything I Wanted. It was easy. I could even run functions on them, etc. I know, in .Net, I can still do all the functions etc., (And the fact that it's treated like an object is good), but I seem to be doing a lot of this, and it's annoying!!!
<asp:Label ID='pnlMyVariable' runat='server'></asp:Panel>
%% CODE BEHIND %%
pnlMyVariable.Text = "Yeah, write this short sentence...";
pnlMyVarialbe.CssClass = "blah";
And don't get me started with the annoying, yet somewhat useful FindControl method!
Is this really the way it is to be? If you say "YES, That's just the way it is", then I will accept it. But surely some people out there (If any are old enough to appreciate Classic ASP) who have seen this to be a little annoying. Am I approaching this correctly?
You're using the asp:Label control correctly, yes. However, it's not a requirement that you use this control. You have lots of other options, but the two which will likely appeal to you the most are:
Use the asp:Literal control. This will work much like a Label but doesn't wrap the value in any additional markup. This allows you to control the markup and styling just like you did "in the old days."
Use the classic syntax. If your page class has a public or protected property on it, that property can be referenced in the markup using the same classic ASP syntax. As a matter of convention in .NET and for a number of good reasons, it's best to use properties instead of directly accessing member variables. But that might end up just being a matter of preference for you.
In webforms, you have this to maintain a model that's similar to winforms. These controls also make use of viewstate, making them hold their values between post backs. MVC is a lot closer to classic asp on the html side, but still very object oriented on the back end. I heavily recommend it!

ASP.NET MVC helper extensions & separation of concerns

When implementing a helper extension for a similar navigation widget to the one described in this article I notice that my HTML is now tied up in C# code, so if I want to re-skin the site with a new menu control that needs slightly different markup (e.g <ul class="foo">...</ul> or distinct class names for nested <li> tags) I now have to edit both the helper extension and the view.
Using a helper extension is quicker and easier, but does this not violate separation of concerns? What are your experiences of maintaining such code? I'm fairly new to Microsoft MVC so forgive me if I'm missing some point here.
A well-designed HTML Helper should be versatile and have very loose opinions about how it builds its HTML. You can add overridable options to the extension method so that the rendering is as configurable as necessary. If you have new requirements and your existing helper isn't flexible enough, extend it to accommodate the new requirements.
I think you are right and have found similar thing. It's the quicker / easier now vs. potential future pain "if" you need to change things in the future. You could possibly make your "helper" read in a template that it uses, so you could change that, restart the web app and see your changes appear. However, that increases complexity too. I would say a lot of agile says do the easier / quicker stuff now and understand that in the future if your requirements change then revisit it then. Who knows, it may never happen.

Is it a Good Practice to Write HTML Using a StringBuilder in my ASP.NET Codebehind?

I'm interested to hear from other developers their opinion on an approach that I typically take. I have a web application, asp.net 2.0, c#.
What I usually do to write out drop downs, tables, input controls, etc. is in the code behind use StringBuilder and write out something like sb.Append("
I don't find myself using to many .net controls as I typically write out the html in the code behind. When I want to use jQuery or call JavaScript I just put that function call in my sb.Append tag like sb.Append("td...onblur='fnCallJS()'.
I've gotten pretty comfortable with this approach. For data access I use EntitySpaces.
I'm just kind of curious if this sort of approach is horribly wrong, ok depending on the context, good, time to learn 3.0, etc. I'm interested in learning and was just looking for some input.
Edit
After reading the comments here it sounds like I should take a look at MVC. I've not done that yet. The only hesitancy in doing so is that the existing project is just that, existing. There is a lot of code already done the way I explained and it is hard to imagine what would be involved in changing it, advantages of doing so, and just learning what that would take.
The other thing I'm taking away from the comments is that my code behind should really not include much of the sb.Append code, whereas now it is filled with it in numerous functions. To me it is not messy but that is because I know what each function does and can look at it and see, oh that writes out x, y, and z.
It's not uncommon for me to just have a div on the .aspx part and then build up the .innerHtml of that with the StringBuilder in the code behind.
Thanks again for the comments. I'm thinking as I'm reading them.
I typically write out the html in the code behind.
That part is a little odd, and not something I recommend for webforms. If you want to do that, consider an asp.net mvc project instead.
In webforms, you really want the meat of your html to live with the markup rather than the code. The two should remain separate. You also don't want a huge stringbuilder that encompasses your entire page. This will force you to keep the entire page in memory twice (once for the stringbuilder bytes and once for the built string at the end) rather than writing the page to the response stream as it's built. That means more memory per request, which can really kill scalability.
To those ends, I would abstract distinct portions of your stringbuilder code into custom/user controls that you can use in the aspx markup. These controls can use a stringbuilder to create their output. This means you only need to keep enough html markup in memory to render one control at a time. It also allows you to more easily re-use common markup across pages or even sites.
There are times when you need to generate some HTML in your code behind, but in general, you want to leave the HTML where it belongs, and that's seperated from your code. The VS IDE is a pretty good HTML editor. Use it.
I'm going to go out on a limb and guess you may have come from a "Classic" ASP (vbScript) or PHP background.
My back ground is "Classic ASP" and my first attempts at the Webforms Model were pretty much the same as yours, once I started usnig them and understanding them I've never looked back. There is a disctinct learning curve though in understanding how the page life cycle interacts with the various WebForm controls.
Look up the various threads on ASP.net WebForms vs MCV to see which suits your projects needs the best. MVC Isn't a magic cure-all but in many respects may be more familiar if you're from a "Classic ASP" or PHP backgound.
From a practical perspective, assuming you're sticking with WebForms, if there is the possibility of other developers becoming involved in the project you aim towards using more of the inbuilt controls where you can as that is more than likely what they will be familiar with. Stating the obvious, the more you use the controls the more you will become familiar with what they can and can't do and before to long you will find yourself writing your own controls to fill the gaps or finding existing 3rd party controls.
A big problem you have with that it can get pretty messy... having to escape all the " or messing with carriage returns. Sure YOU can program around that, but what if you want to copy/paste code? sounds like a nightmare and WAY more work than it's worth.
It sounds like you should be writing a custom control and using HtmlTextWriter to write the markup.
Or perhaps more appropriate would be a user control, with markup in the aspx page and anything else in the code behind.
If you're using this approach, you should migrate your development efforts to ASP.Net MVC. Whereas ASP.Net actively tries to abstract the HTML, CSS, JavaScript, etc. away by using web controls, ASP.Net MVC is built around a paradigm of directly controlling the markup itself (though that may arguably be the least of the differences between the two - you should definitely read up on it to at least know the alternatives, even if you stick with ASP.Net in the long run).
Otherwise, what you're doing works if done properly (though you'll be fighting the framework the whole way), though I'd recommend using a StringWriter instead. It uses a StringBuilder internally so the performance characteristics are the same between the two, but the semantics are more consistent with the rest of the .Net framework (e.g., Write vs. Append).
I think this approach kind of defeats the purpose of what webforms was trying to accomplish (separating markup and code).
I know this thread is kind of old and has been answered really well, I just thought I would "append" (pun intended) my answer since I am working with code that was mentioned in the question.
ALL the markup is in the C# classes and they created a StringBuilder object to append all the html and JavaScript strings. This has made it very difficult to read the code and see what's going on, and what if they want to change the markup/design of the front-end? Now, I've got a heck of job on my hands having to go in and refactor all that markup in the classes, when it would be so much easier to change the .aspx pages and connect the data model to those pages.
In my humble opinion, I can't find a good reason to put any markup in your classes/code behind. They are for logic only. Plus, it makes it difficult to test and debug Javascript. That's my two cents. K.

What are bloating controls

What are bloating controls?
Can anyone please explain it?
It's a term which can be used to describe a variety of things, e.g. it is commonly used by critics of ASP.NET WebForms where, if you are making heavy use of pre-built WebControls you have little control of the generated HTML, which in many cases appears 'bloated' (large) and 'unclean', 'messy', etc.
I'm not sure 'bloating' is exactly what you mean. Some controls could be called 'bloated'.
In that case it means that some controls offer a great deal of functionality which in most cases is not required. Grid controls tend to be fairly bloated because they expose functionality for sorting, filtering, binding, templating etc.. but it's unlikely that you'll use all of this in a single implementation of the grid so you're taking up much more memory than you actually need which could potentially cause performance issues.
Hope this helps.
I guess people are referring to the fact that some GUI controls are very big and thus a bit hard to understand / use.

C# penalty for number of lines of code?

Are there limits or performance penalties on the amount of code inside of my home.cs form?
I am writing a database application front-end in C# in Visual Studio 2008. The way things are lining up, I am using a tab-page way of changing the info shown to the end users, instead of using new forms.
Coming from VBA/MS Access, I remember that if you go over a certain number of lines of code, it would produce an error and not compile. Will C# do this in Visual Studio 2008, or will I suffer a performance hit? I know code readability could be a problem because everything would be in one place, but I can also see that as an advantage in some situations.
It's not the lines of code in your .cs files that you need to be worried about with regards to performance - it's the number of controls on your form at runtime that might cause problems. If it's just a few controls on a few tabs, you will have no problems. If it's hundreds of controls on lots of tabs, you may have performance problems (not to mention usability problems - I personally hate tab controls with more than one row of tabs).
Also, I don't think tabs are appropriate if the purpose of the UI is more wizard-like where you want the user to interact with all of the tabs in succession. Tabs are meant for presenting sets of options to the user, without requiring them to see all the options at once.
Finally, if the purpose of each tab is significantly different, I find that it's easier to encapsulate each bit of functionality as a separate form. With tabs, you could at least encapsulate each bit as usercontrols, and then have each tab on your form host one instance of a usercontrol.
The only problem i would foresee is that in the future its going to be very hard to maintain.
Try break the logic of that main form up as much as possible into classes so that when you need add something you can actually do it without having a fit.
If you are using tabs, you can still create custom user controls that will hold the content that goes in the tabs. Make a control per tab, and then you can keep your code for the different tabs separate. There is a walk-through on MSDN here.
In response to your comment above, about not showing the tabs, I would really re-think how you're approaching this. Why not simply have all of your user controls sitting on your main form, in a Panel if necessary, have them all set to Dock = DockStyle.Fill, and then change the Visible and Enabled properties based on which one you want to show? You may be making this harder on yourself than it needs to be.
More responses to comments - You may be looking for something like the CardLayout in Java. The source for the GNU Classpath version can be found here, it might give you some ideas on how to implement this.
"I know code readability could be a problem because everything would be in one place, but I can also see that as an advantage in some situations."
In my experience, this attitude will ultimately leave anyone who has to maintain your code in the future with quite a headache, as it's an accepted practice to modularize your code so that the pieces that may change or the ones that serve distinctly different purposes are separated away from each other.
With that said, I don't think there is a limit imposed by VS on the length of your files, but I think you will run into some seriously frustrating performance degradation as your files become longer, especially while switching between design and code views.
I would urge you to save your future self and his/her sanity and break your code up logically into separate files. You'll thank yourself later!
It shouldn't be a problem.
Just keep good coding practices in mind and modularise your code for readability and maintainability.
On the other hand, if you put too many controls on your form, then it will probably take longer to load. Factor that into your design if you want a snappy interface.
Sounds horrible but I don't see any reason why it would be a problem.
I have a form that in inherited with my new job that had over 30,000 Lines. It is completely cancerous. Please think before you code and modularize!

Categories

Resources