Use a class to update data? - c#

I have searched for this topic, and can't find anything that answers my needs, so I am assuming I may be looking at this the wrong way. Keep in mind, I was a classic ASP developer for 13 years, so I am used to having control over all of my code.
I have many forms on my sites for updating data: Customer information, Supplier information, Product information, User information, etc., etc. I used a library I wrote which made it easy to simply build a form, set the values, and it created the update script automatically. However, in ASP.Net, it just doesn't seem this is the way to do it.
ASP.Net, seems to have wizards. Personally, I don't like that approach. I like full control. I don't even like the ASP.Net validation controls...
Option 1: Build classes with update methods. Build a standard ASP.Net form, which will update the data through the class?
Option 2: Use the annoying GridViews, DataAdapters, wizards, etc., etc.
Option 3: Standard form, with AJAX, calling a WebMethod with the class to update data.
Which approach is most standard now (or most practical)? Which one has the best control as well as security? I don't like wizards, and I like to know exactly what's going on, so when something "doesn't go on", I can figure out why without going through a wizard. I want to be efficient with creating forms, so I can create them quickly but still have the flexibility to customize and control what's going on from user interface, to validation, to security.

It sounds like you want something like ASP.NET MVC instead of WebForms. It gives you much more fine-tuned control over the HTML content you produce, but at the same time it has some very flexible and powerful features that make it easy to perform common tasks.

You should look at things like FormView and DetailsView. They work with data source controls like the SqlDataSource control to automatically generate the SQL to interact with the database. No code involved.

If you already have a ASP Classic library, can you not just port that to a .Net library and continue using it. If you have code that works and you are happy with it no need to throw it away just because it might not be exactly the "correct path"

Related

c# .Net Visual Studio show execution path that lead to page display

I have inherited a mostly c# MVC application that has been around for years and is quite the collection of mySQL, stored procs, Web Forms, MVC, WebAPI, JS, jQuery, Angular, and I think there is some React in there as well. I am looking for a tool where I can start the application, get to a particular page, then look at what classes and methods were called to get there.
I know the application needs refactored and, frankly, completely rewritten, and that is happening. We just also need to support this tool as another team develops the next version.
Thanks,
Sammer
You can use my Runtime Flow tool to quickly find C# classes and methods responsible to display a particular page.

Question about using CreateWizard and Login Controls as opposed to starting from scratch

I am just learning C# ASP.NET, when I create a project in visual studio, the login controls and CreateWizard is standard. Do many ADVANCED programmers use these things or do they just build from scratch? If you dont use this as your login method, how are you sure that you will take all the correct security measures of being hacked, sql injection etc etc.? I feel like its easy to use the login controls and createwizard, i couldnt see why anyone else would not use it?
Sometimes it can take more effort to learn how to use these prebuilt controls at the start, but with them you gain the advantage of using proven industry standards, built in validation, etc.
Often a custom solution can overlook possible bugs. The built in controls have likely be more throughly tested then any testing that could be done to any custom controls that are created, with known bugs being well documented and likely already found within the community.
These controls should not be thought of as closed solutions as a lot of these controls are built to be easily extended by the use of the provider patterns (such as the Membership Provider), theming and templates, allowing for custom functionality when needed.
In all my recent projects, I've just created both pages from scratch.
Part of it is that I want more control over how my pages look, and part of it is my mentality where I often find it easier to just do something my way rather than refreshing myself on how those controls work.
By default, ASP.NET pages raise an exception if fields are submitted with HTML codes. However, even this is not an issue. Just make sure you run any user-entered text through HttpUtility.HtmlEncode() whenever you display it on a page.

Which one is better apporach comet or ajax?

i need to update client on any changes occurred on server.
for that i found 2 approach.
1. using ajax which is also known as reverse ajax for this purpose.
2. using COMET.
but i don't know exact difference in both.
my site contains news content and i want that news to be automatically updates when new news is entered by my CMS application.
i have got hundreds of concurrent users on my web application.
please suggest me which approach should i use to get best solution.
also please provide me good example's like for that so that i can implement it.
NOTE: i am using .net framework 2.0 but if its not possible in 2.0 then can also move to 3.5
Thanks.
First start with YAGNI principle. See if it is ok for your client to update periodically, lets say every min or every 30 seconds. Consider things like how much information users can process in this interval. Also take a look at popular news site and how they implement this feature. In this case you are better off using the pull model where your client updates itself by requesting data from server. Implementing a push model is much more complicated.
Once you are sure that you need the push model, IMO comet is a better option. Coment is designed for this purpose. Dojo Foundation's CometD is a widely used library for this purpose. One good example is the live chess application on chess.com
Also, though I am not 100% sure about this, I believe you will need to use technologies like ASP.NET MVC which will allow you more control on the markup generated by your web app so that you can use these libraries.

ASP.NET and Objects

I've been tasked with a project that requires me to convert a quote for a set of products that is displayed online into a particular file format for import into a third party application. All of the information I need is stored in a database that I can easily access.
Unfortunately, I absolutely have to offer this as a web interface and they want it to be a natural extension of their current ASP.NET product.
Even more unfortunate is that I've had no prior experience in ASP.NET and, as a result, I can't seem to phrase my question in a way that gets the desire result in search engines. I guess I'm behind on the proper terminology.
What I'd like to do is to take the data in the database and read it into objects that I've created in C# that hold their particular necessary attributes. For instance:
The QUOTE class contains a list of ITEM attributes.
The ITEM class contains a list of MODIFICATIONs.
I could then just loop over all the line items and output the necessary information to perform the task.
I'm familiar with Ruby on Rails and how I can set up objects, work on them, and then reference them in the view, but I'm completely lost in ASP.NET.
So, the short version is this:
What am I trying to do in terms of ASP.NET terminology so that I can research how to do it?
Is it even possible?
You will need two things that will help you out tremendously, for this project and for the next ones:
Learn a bit of LINQ, specifically Linq to SQL. The best book is "Linq in Action" by Manning. LINQ will open up a whole new horizon on data access and will make your like easier.
Follow the ASP .NET MVC tutorial at NerdDinner.com. It will get you familiarized with MVC in a few hours.
You need less than a week to get you up to speed, and then the project will suddenly look a lot more approachable.
If you are familiar with Rails, then you will want to look at ASP.Net MVC
You need an object-relational mapper, or ORM. The most popular one is NHibernate, and some folks have created a framework called ActiveRecord that sits atop it and acts sort of like the activerecord you're probably familiar with in Rails. Start your Googling there, with NHibernate.
they want it to be a natural
extension of their current ASP.NET
product.
Depending what platform they are in right now, if it's asp.net webform, then I suggest you look into GridView control since it's very powerful to display the grid Data.
If you go for Asp.Net mvc which is closer to Rails, then you may look into something like jqGrid.
First thing to do is define more clearly what you need to do.
It sounds like you need to add functionality to an existing web app that "displays a quote for a set of products" (like a shopping cart page), that is already saved in a database.
The functionality you need to add (I think), is a feature to convert the information for the quote from the database - into a particular file format (specified by the third party app that will import the information).
Is that correct, or is the scope of your task larger / smaller?

Controls versus standard HTML

I'm getting into ASP.NET (C# - I know it doesn't matter for this particular question, but full disclosure and all that), and while I love that the asp:-style controls save me a lot of tedious HTML-crafting, I am often frustrated with certain behaviors. I encountered one last night when working with Master Pages: my <asp:BulletedList ID="nav">, when converted into HTML, became <ul id="ct100_nav">.
There are other issues--I noticed that when you auto-populate a DataGrid, it adds attributes to the resulting table that I don't necessarily want there.
I know that there is a certain amount of "convention over configuration" that you have to accept when you rely on a framework to take over some of your tedious duties, but the "conventions" in these cases aren't so much any established conventions, but rather unnecessary extras. I know why the ID adds the prefix, but I should be able to tweak and turn things like this off, especially since, as a bit of a web standards evangelist, I don't duplicated HTML id's in a single page anyway.
So the question here is for those ASP.NET devs more seasoned than I: in your experiences in developing and deploying apps, how do you leverage these controls? Do you find yourself resorting back to hard-coded HTML? Do you use a blend? I don't want to design my HTML around idiosyncratic quirks in these controls, but, if possible, I'd like to leverage them when possible.
What's a boy to do?
Personally,
I think the standard ASP.NET controls are fine for inhouse stuff - quick and dirty is good in that scenario. But, I once worked with a web developer who was also a designer and he refused to use the ASP.NET controls and only code in HTML and add runat="server" tags when needed. This was more because he wanted to know exactly how his HTML was going to be rendered, and at the time anyway, some of the ASP.NET controls wouldn't render to standards compliance.
I sit somewhere in the middle - use HTML where appropriate and not when not. You can sort of best of both worlds with the CSS control Adapters
I'm actually quite relieved to see some opinions here agreeing with my own: ASP.NET as a template language is very poor.
I'd just like to rebut a couple of the pro points made here (flamesuit on!):
Dave Ward mentions ID collisions - this is true, but my how badly handled. I would have preferred to see nodes referenced by xpath or deep css selectors than by making the ID effectively useless except by deferring to ASP.NET internals like clientID - it just makes writing CSS and JS that much harder pointlessly.
Rob Cooper talks about how the controls are a replacement for HTML so it's all fine (paraphrasing, forgive me Rob) - well it's not fine, because they took an existing and well understood language and said "no, you have to do things our way now", and their way is very poorly implemented. e.g. asp:panel renders a table in one browser and a div in another! Without documentation or execution, the markup for a login control (and many others) isn't predictable. How are you going to get a designer to write CSS against that?
Espo writes about how controls give you the benefits of abstraction if the platform changes the html - well this is clearly circular (It's only changing because the platform is changing, and wouldn't need to if I just had my own HTML there instead) and actually creates a problem. If the control is going to change with updates again how is my CSS supposed to cope with that?
Apologists will say "yes but you can change this in the config" or talk about overriding controls and custom controls. Well why should I have to? The css friendly controls package meant to fix some of these problems is anything but with it's unsemantic markup and it doesn't address the ID issue.
It's impossible to implement MVC (the abstract concept, not the 3.5 implementation) out of the box with webform apps becuase these controls so tightly bind the view and control. There's a barrier of entry for the traditional web designer now because he has to get involved with server side code to implement what used to be the separate domains of CSS and JS. I sympathise with these people.
I do strongly agree with Kiwi's point that controls allow for some very rapid development for apps of a certain profile, and I accept that for whatever reason some programmers find HTML unpleasant, and further that the advantages the other parts of ASP.NET give you, which requires these controls, may be worth the price.
However, I resent the loss of control, I feel the model of dealing with things like classes, styles and scripting on the codebehind is a wrongheaded step backwards, and I further feel that there are better models for templating (implementation of microformats and xslt for this platform) although replacing controls with these is non-trivial.
I think ASP.NET could learn a lot from related tech in LAMP and rails world, until then I hope to work with 3.5 MVC where I can.
(sorry that was so long </rant>)
The short answer is that you should never use an asp:... version of a standard HTML control unless you have a really good reason.
Junior developers often get suckered into using those controls because they're covered in most ASP.NET books, so it's assumed that they must be better. They're not. At this point, after 8 years of daily ASP.NET development, I can only think of 2 or 3 cases where it actually makes sense to use an asp:... INPUT control over a standard HTML one.
As for the ID's on server-controls: You can find the actually ID that is going to be written to the browser by accessing ClientID. That way you can combine server-side og client-side scripting and still dont have to hardcode _id="ct100_nav"_
I always try to use the included controls instead of "hacking" HTML, because if there is an update or some improvement later on, all my code will still work by just replacing the framework and I don't have to change any HTML.
Hope this helps
#Brian,
Yup! You can pretty much control all the behaviour.. Consider looking into creating Custom Controls (there are three types). I recently gave an overview of them in my question here.
I would strongly recommend checking them out, has help me no end :)
I too am on my adventure into ASP.NET and have also had similar frustrations.. However, you soon get used to it. You just need to remember, the reason you dont have the tedious HTML crafting is because the ASP.NET controls do it all for you.
To some extent you can control/tweak these things, even if it means inheriting the control and tweaking the HTML output from there.
I have had to do that in the past, where certain controls were not passing W3C validation by default by putting some extra markup here and there, so I simply overrode and edited as necessary (a fix that too literally a couple of minutes)..
I would say learn about how the controls system works.. Then knock a few together yourself, this has really helped me grok whats going on under the hood, so if I ever get any problems, I have an idea where to go.
The HTML renders with those sort of IDs because its ASP.NET's way of preventing ID collisions. Each container control, such as a Master page or Wizard control, will prepend an "ID_" on its childrens' IDs.
In the case of your bullet list, the ListView provides a nice middle ground. You can still bind it to a datasource, but it gives you much tighter control over the rendered HTML. Scott Gu has a nice intro to the ListView here:
http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx
If the ID's prefix added by ASP.NET is an issue for you to access them later using JS or something... you have the .ClientID property server side.
If the overhead added by ASP.NET you should consider ASP.NET MVC (still preview) where you have full control over the emitted html.
I'm moving to MVC because I don't like all that stuffs added too....
I think most of the answers here take a designer's point of view. On a small-to-medium project it might seem like an overhead to synchronize code and CSS/HTML and make them standards-compliant and clean. A designer's way to do that is to have full control over rendered HTML. But there's many ways to have that full control in ASP.NET. And for me, having the required HTML in the aspx/ascx file is the most non-scalable and dirty way to do it. If you want to style controls through CSS, you can always set a class server-side through the CssClass property. If you want to access them through JS, you can emit the JS with right IDs server-side again. The only disadvantage that this provides is that a dev and a designer have to work together closely. On any large project this is unavoidable anyway. But the advantages ASP.NET provides far outnumber these difficulties.
Still, if you want standards-compliant HTML, skinning support and other goodies to control rendered markup, you can always use thrid-party controls.
As Dave Ward has already mentioned, "it's ASP.NET's way of preventing ID collisions."
A very good example of this is if you're attempting to put a control inside of a custom control, then use that custom control in a repeater, so that custom control's HTML would be output multiple times for the page.
As others have mentioned, if you need to access the controls for javascript, use the ClientScript property which will give you access to a ClientScriptManager and register your scripts to the page this way. Be sure when writing your scripts to use the ClientID property on the control you're trying to reference instead of just typing in the control's ID.
If you want that much control over the rendered HTML, look into ASP.NET MVC instead.

Categories