Hi there, I've been rummaging through the web but did not come up with anything satisfier. Does anyone have a knowledge whether coding a webpage using only code behind is a good way of creating a website? I mean something as follows: The .aspx file is something like ...//aspx codes <html> <body> <%=_out %> </body> </html> And the .cs file holds all the code. In the page_load event I define the content of the string _out and voila the website is coded. For instance Page_load event(object sender, event e){ _out = "this is my webpage, <a href=\"#\">click here</a> if you like it"; } I re-pose my question: Is coding a webpage using only c# like above a valid structure for coding ?
You would normally use an <asp:Literal /> rather than <%= %> in the example given Certainly using code behind is one of the core benefits of using asp.Net as it allows the programming/ heavy work, to be precompiled into MSIL which is much more efficient than an interpreted scripting language. If you did all your programming in .Net as inline scripting you would hardly be any better off than if you were running classic ASP
Generally speaking, I would say no. If the page content is only a few words, then it is fine to put that content in an aspnet control. If the content is a typical webpage, wrapping all of it in c# makes it very difficult to work with and to maintain. It is contrary to good programming practices. I would look at it and say "this guy had no idea what he was doing."
in some cases programmers use those. but in most cases we cant. coding on the code behind page gives lot more flexibility.
I appreciate your answers. I changed the whole structure of my website parallel with your suggestions and I figured that out that I was missing a lot of .net features. Thanks again
No it is not the right way. HTML or ASP code should be in aspx file. logic should be in .cs file. Javascript should be in .js file or in aspx when you need to use server-generated ids. Try to add runat=server attribute and id=myTagId to any of HTML tags and you will be able to access all its properties and events by typing myTagId.<property or event name> in your code behind file.
even most of time we use code behind for logic implementation also some of case have to move for those coding both are same and there are no any special advantages or disadvantages but if you always using code behind (.cs) for to write your logic code will be very clean and readable.
if the text you are going for is static and is no dependent by anything else just put it in the aspx/ascx files ... if you side is data driven or uses resource files for multi-language your better using the code behind and creating .net controls.
You can put the design part in C# code but its' not the prefered way of doing it. The design should be in the aspx file and the business logic should be in the C#. It makes page design way more easier.
I always a simple rule of thumb, if it's design it should be in my aspx or css pages, if it's logic it should be in code behind or business layer
I think, you should use the simpler ways of .cs files. Use libraries for the same, it will ease out the coding