Converting WinForms App to WebForm App - asp.net

Previously i was working on Windows Form based application in .Net. Now i am developing the same app in ASP.Net.
The issue is, in WinForms, i had functionality which is like this:-
ListView was present in the MainForm. When a user clicks on that ListView, another form was going to pop-up, which is also a WinForm. (For this form i was sending a Bill Object as reference)
In the pop-up form, the user will select a particular Billable Item and click on a button called "Add".(This Billable Item is going to be saved in Bill Object)
When this button is clicked, an event is fired in the MainForm. This event was going to add the Billable Item (the one which user has selected in the pop-up Form) into the ListView using the 'Bill Object', without even closing the pop-up form.
This procedure will continue until the user manually closes the pop-up Form.
My issue is, i do not know how to realize this or make this work in Web Application using ASP.Net with vb in code behind. I am using Content Page.
I know i can use GridView in asp.net, just like ListView in WinForm.
Please give your valuable suggestions.
Thank You,
Mayur

ASP.net was designed to be familiar to WinForms developers which is why you have many of the same control names. In actuality, this is often a hindrance as there are significant differences between many ASP.net webcontrols and their WinForms counterparts.
In addition the page lifecycle that is at the heart of ASP.net webforms is a very different model to what you would be used to with WinForms.
I suggest starting with the official ASP.net learning resources. They're a great introduction to the two main ASP.net approaches:
Webforms: http://www.asp.net/web-forms
MVC: http://www.asp.net/mvc

Related

WebForms Transferring To MVC

I was wanting to ask the following about mvc to have a better understanding.
1 What is the difference between the way webforms and action controllers work.
2 How should one coming from a webforms background convert the page_load etc button clicks etc into mvc methods and events. Its this understanding that I am lacking.
3.How do i fill controls before I was used to setting the datasource but see allot of controls using the foreach on the front end is that really code separation?.
4 I will be developing a form designer in .net webforms I was used to using panels and loading controls but I see it will be neater in mvc by using partial views would that be my best course of action. I am a senior asp.net webforms developer with over ten years experience.
I have been watching plurisight videos but they center on using sql express not server.
A lot of help material you can get online about this topic.
Your first question about the way webforms and action controllers work -
In webform, you specify the code-behind file of your .aspx page and the code-behind file is the master of that page now. The browser hits the .aspx and the code behind file manages the work.
But in MVC, no view file is approached; the path is matched to the respective controller and action and the action handles it. Any controller can access the View of any other Controller. There are Shared Views which are common for every controller as well.
I strongly suggest you to read at this link and this codeproject article.
Some major points will be this :
You wont have the RAD (Rapid Application Development) environment i.e. the drag drop support for Controls, page viewer in case of Razor, etc.
You wont have the basic server controls Gridview, Repeater,etc in MVC. None of the controls in MVC are bind to any controller. You can neatly pick the desired elements using Javascript and play with them.
You get full control over the HTML
What I feel is that MVC is more flexible and jQuery-able as compared to Webforms
All the best!

ASP.NET Web Form Render Engine outputs a Control Tree? Looking for info on render logic

I've been watching a video on Scott Hanselmnn teaching MVC 2 tricks/tips. He mentions how MVC 2 by default uses ASP.NET Web Forms view engine to render the output of the views; he mentions that the web forms view engine is a little slower than it could be for MVC 2 since it generates a control tree and then outputs the HTML to the page (I hope I said that right).
I was wondering what he meant by web forms generating a code tree before outputting the HTML to the page. Does anyone have insight on the view engine of Web forms and the steps of the rendering process works for ASP.NET and MVC2?
In Web Forms, the HTML is generated by a hierarchy of controls, each of which needs to be called upon to render its HTML and each of which contributes to the Page ViewState. In addition, a lot of events are fired by Web Forms (Init, PreRender, etc) during its life cycle, and each control in the hierarchy also fires similar events.
In MVC, the process could theoretically be much simpler, as you don't have a deep hierarchy of controls, you don't have ViewState, and you don't have the need to fire events. However, MVC "piggybacks" off of the ASP.NET framework, and so behind the scenes, a lot of the Web Forms stuff is still there, although it's not needed.
ASP.Net WebForms was all built around the idea of "Faking" a persitent, stateful model around the stateless nature of HTTP. The idea was to give WinForms developers a familiar environment to work in, i.e. Controls, Events, Etc...
In order to do this, the markup is parsed into a collection of objects in memory that you can then reference like you would a control in WinForms:
TextBox.Text = "I hate viewstate!";
Each Control is added to a collection of Controls that represent the page to be sent back to the client. When it comes time to build the response, the engine walks through the tree collection of controls and asks each control to Render itself to the output stream. The result is what you get in the form of an HTTP Response.
In MVC this is an unnecessary step because those controls are never referenced. MVC embraces the stateless nature of the web, and instead maps posted form variables directly to Models for use by Controller Actions.

ASP.NET (webforms): Using with MINIMAL server controls and substituting with JQUERY?

I am currently working with ASP.NET and the person who designed the form has used all Server Controls for things like TextBoxes and Dropdowns etc when really they are not providing postbacks.. Some of the dropdowns and textboxes are values that I need only in jQuery so as far as I can see there are no drawbacks to coverting these controls to standard html controls rather than ASP.NET server controls?
I suppose I will need to continue to have my GetDataGrid button as a server control because I will need it to postback (and receive PageLoad events etc - all asp.net events) to update the GridView? Or would it be possible to use the GridView (ASP.NET server control) from a Webmethod and call it via Jquery?
Of course in my webmethod I would need to the instance of the gridview to add the datasource - but I don't see how this would be possible without being in the ASP.NET events - or maybe I wrong?
The other thing I thought of was changing the GetGridView button to a standard HTML and calling the javascript postback from the client click event?? This way it would do a real postback and I would end up in Page_load.
Taking everything into effect i don't want to the change the GridView asp.net control as it funcions well as an asp.net server control but i am unsure how i would do this.
I remember a document being available that said "how to use asp.net webforms without server controls" but i can't seem to find it. I suppose using webforms like asp.net MVC - but i can't change the project to MVC - its out of my control.
I would love to hear some feedback with regards to how to do this or comments etc.
I find ASP.NET webforms to inject a lot of code smell into pages - I am using .NET 3.5 so a lot of the output is with tables etc...
If you use Request.Form["..."] then you can get the information which was filled in in standard html input fields.
Instead of keep on using the GridView control I suggest you take a look at either jqGrid or the new templating system that Microsoft put into place for jQuery (currently a plugin but expected to be part of core jQuery from version 1.5 on). These can bound to json which can be retrieved from a webmethod or pagemethod call to fill up the template with data.
Also i don't think its possible from asp.net (code behind) to receive values of an html >control without it having runat=server.
Use webmethods.
Set a client event (like 'onchange') on the html control and then in javascript function called when the event is fired you can use PageMethods to send your data to the code behind.
Some thoughts...
The GridView can't be created in a WebMethod and even if there was a way to get that to work, you'd be better off going with a genuine client side grid. As that's not an option, I don't think there is too much point in trying to make any major changes to your existing pages.
ViewState
Changing the textboxes, buttons etc to HTML versions, would gain you a little bit in reduced Viewstate size but add a bit of complexity in how you handle interactions with the page. You can add runat="server" to HTML controls which will give you control over what is rendered and still have access to the control on the server side.
.Net 4 gives you far more control over viewstate but unfortunately in 3.5 its not as easy.
The GridViews
You could wrap the GridViews in UpdatePanels. That's a 'cheap' way to add some interactivity to your pages although you won't be gaining anything in terms of performance.
It's also still possible to manipulate the Gridview using jQuery on the client-side. There a lots of tutorials, blog posts etc explaining how to do this on the Internet.
MVC with Webforms
Its also possible to mix ASP.Net MVC with Webforms in the same website. As it sounds like you are familiar weith MVC, you might want to consider this approach for any new pages. Here's a blog post explaining how to do this.
Update:
Here's a more recent article by Scott Hanselman on how to use MVC with an existing Webforms application.

MVC Custom Control?

I am trying to figure out how to use/create a custom control in ASP.NET MVC 2.
I created a custom control earlier and compiled it (ccontrol.dll), the control renders a div, textbox and a button + some javascript in order to post a comment on the website. It could be a static aspx page that i wanted to allow my visitors to add a comment to. I would then drag my control from the toolbar to the aspx page and run it, it would then render all the code needed on the webpage including fetching the data from a datasource and displaying that inside the div. The user could also just type in a comment and press the button to save it to the datasource.
Is this possible to convert to MVC 2? Any good tutorial that covers custom controls and MVC 2? (Ideally would be if the control could be made into a .dll file that i then could reuse on future webpages)
How do i write a custom control the mvc way? Any good tutorials on the topic?
You cannot design Custom Controls according the normal asp.net style because in Mvc there is no ViewState and there are no server side control events. Data are returned back to the server through a Model Binding process. The fact that rendering and filling data in are handled in separated pieces of code make difficult to implement complex server controls in Mvc.
However, I developed a theory, and also a toolset to make quite easily custom controls ina Mvc too in the full spirit of the Mvc paradigm i.e keeping separation of concerns between Views and Controllers. See My Codeplex project. There, you will find pointers to documentation and tutorials on my blog. If you need assistance feel free to contact me.
No it is not possible to use custom controls in ASP.NET MVC. you need to re-write in MVC way

ASP.NET MVP - Utilizing User Controls

I'm writing my first app with ASP.NET MVP (attempting Supervisory Controller) and Unit Testing (better late than never!), and I've run into a bit of a dilemma. I've written 3 User Controls, all tested and Interfaced up, Presenters in tow. Now I have come to a Page which takes these three User Controls and encountered the following problem:
User Control 1 is DateSelector, it's a Calendar control with a couple of other buttons/lists. Nothing real exciting.
User Control 2 is DailyList. Guess what it is. Anyway, you can select/delete etc.. if you select an item from the gridview, it needs to populate User Control 3.
User Control 3 is ItemDetail. Here are the DropDownLists, TextBoxes, etc... some with existing dependencies on others (selecting an option in DropDown one affects the options in DropDown 2).
If I select a new Date from my DateSelector, do I raise the event from the DateSelector Presenter? I have to somehow let the other User Controls know that a new date was selected, so that they can rebind their data, but how? If I use the Page's Presenter to subscribe to the Presenters of the User Control Views, will I not be blatantly breaking the Law of Demeter (exposing the Presenters as Properties through their Views)? Mustn't I use the Page's Presenter as the All-Knowing Controller of the page? Is there something I am missing?
Everything I've read so far says, "MVP is great, even with User Controls", but the use of User Controls is conveniently forgotten about when it comes to examples. It appears to me MVC would more closely match my pattern of thinking on this one, but currently, MVC is not an option. Any help would be great here. Thanks in advance.
The Page Presenter has to be the one to coordinate the interactions between the controls on the page. Who else would do it? I'd have the page presenter listen in on the event of the DateSelector user control. From the page presenter's perspective, it probably doesn't need to know who raised the event (view or presenter) as it looks at the DateSelector as a fully encapsulated control. The internal workings should be hidden from the page's presenter.
All it knows is that the DateSelector user control raised an event, and now it needs to notify the other controls on the page, either by raising its own event, or by explicitly calling methods on the presenter.

Resources