OK so I have a webforms project I am wanting to start doing MVC instead. I would like the MVC pages to use the Webforms master page until I can get things transferred over more to MVC. I have this working with a solution I have found you can find listed below (This project works really well except the server event)
https://github.com/MicahArmantrout/BlackMagicMVC
My problem is that server side events in the master page are not firing. When you load the project up if you click on "Click Me!" the button will fire a server event and the Text will change to "You clicked me".
If you goto "Click For MVC" (Which is a MVC Partial Render) and then click the "Click Me!" the server event never fires. Any idea why and if this can be fixed ?
Note: This is not just a exercise I have a place in a application where this would be extremely useful.If you want to understand this project more please see
http://trycatchfail.com/blog/post/ASPNET-MVC-3-Razor-C-and-VBNET-WebForms-A-Tale-of-Black-Magic-Voodoo.aspx
There is no concept of server side events in MVC. And MVC is not going to wire up the client side events and translate them to server side event handlers the way ASP.Net WebForms does.
You have these options:
Recreate master page in MVC (_Layout.cshtml) and then handle button clicks in your controller. Depending on your design, some refactoring would be needed. You need to decide how or who handles those button clicks. You can probably get away with handling some of them in a Base Controller, but I would suggest to take a second look at what you're doing in master and how you can translate them on MVC.
Do the other way round - define you master page in MVC, and render that in your webforms.
Ideally, your master page should not be having any business functions anyway. It should be nothing more than Navigation and style. As long as you adhere to this, migrating to MVC won't be that painful.
Related
This is just a curious (potentially stupid) question. I am fairly new to ASP.NET but...
I was testing out some functionality on my ASP.NET Web forms page and every time I test I have to select some options on a few drop-down lists and hit a submit button, at which point the triggers a post-back.
The select some options on a few drop-down lists and hit a submit button action feels mundane and I wanted to know... what changes client-side when you trigger a post-back? and... is there anything I can do in the browser/inspect tool that will return to me the same page view/result of the post-back without select some options on a few drop-down lists and hit a submit button without data maintenance through page life-cycle (Session/ViewState).
I was thinking the only way was to copy the entire HTML on the triggered control event page and have it render that?
An ASP.NET server injects a system JavaScript block with the __doPostBack function. Each postback control on a page calls this function with corresponding parameters. See Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net for details.
The server generates a full page during a postback and sends it in HTTP response body to the client. As a result, the entire page is refreshed in a browser. If you do not want to send full page postbacks, I suggest you use UpdatePanel:
UpdatePanel Control Overview
Introduction to the UpdatePanel Control
Hi I am just learning about C# can anyone recommend a site I can look at and look at the development side of the site to show me just how the C# language is applied to the site. Eg. is it just added to the HTML like JS or how is it worked into the coding. I just can't get my head around how it comes together with HTML, CSS and C#.
Thanks Linda
Creating web applications with C# comes in 2 flavors. Web forms applications and MVC applications. Web forms is the oldest and was the first. It works based on events. You would design a form or a "page", drag controls onto the page (such as a text box or a button), then wire them up to do something based on some event. For example, if you had a button on your application, you would respond to the Click event. The events were server side and would cause a post back. After the post back, your event, such as Click would be executed. This code was in C#.
Because this event based program was cludgy and ugly and didn't really "flow" correctly with how web applications work, MVC came along. MVC (Model-View-Controller) works differently. When someone in their browser put in your URL (http://www.example.com), IIS would execute your controller that handles the page they are requesting. Your controller would optionally load data from a database, put it into a model, then pass the model to your view. The view is your UI and it receives data from the model and does "something". Any new web application you make these days will almost always be an MVC application.
I hope this helps!
Try the ASP.Net MVC framework.
http://www.w3schools.com/aspnet/mvc_intro.asp is a good tutorial to get you started with using it. It is slightly outdated, but still gives a good intro to how it integrates with HTML.
I want to make a dynamic control on a web page that does an action and generates new HTML code without needing to refresh the page (what would traditionally be handled by JavaScript, I presume). However, my project is currently comfortably and neatly nestled in as a pure ASP.NET Web Pages project strictly using Razor, no MVC at all.
I've hunted everywhere and haven't gotten a clear answer; what answers I have found always concern deep MVC or Web Forms components. As I understand, it should be fairly easy to make use of bits and pieces of those, but I'm still fairly inexperienced with ASP.NET.
What I want to make in short: A button that
Is an image
When clicked, increments a record value (already stored in the Razor code, simple to push to the database), and replaces itself with another image button
This button has a different image, will decrement the record value and replace itself with the original button (they are effectively the inverse of each other)
This is the most complex control I need at the moment for my project, and learning about this will hopefully provide a basis from which to engineer different controls.
The underlying technology of the server is irrelevant, whether it's PHP, ASP.NET, Ruby, et cetera - if you want a web page to "do something", you need to send a request to the server. If you want it to do something without the user directly submitting a form or clicking on a link and changing their location, you have to send the request to your server with javascript. Javascript is how you get the client to do things. The razor templating system operates on the server, not on the client.
Specifically, you should look in to Javascript AJAX requests; I'd particularly recommend learning about jQuery, because it simplifies a lot of aspects of Javascript.
I am trying to implement a KeyDown event for a textbox in Visual Web Developer. I am using C#. I know how to do this in a windows form but the technique isn't portable to VWD. I want to capture the text in the textbox when the user hits Enter.
Any advice is appreciated.
Regards.
Sounds like you may want to read up a bit on Web forms in general. A quick summary:
Since web pages are all client side, you have to explicitly tell it when to talk to the server where all the major lifting takes place.
So you have the html form tags:
<form>
</form>
and all important text boxes and other form controls go between.
Then you need a submit button which under normal circumstances is the only way to submit the form to the server for processing. (The "enter" key activates the submit key also.). Submission always either reloads the page or causes a move to the next page, depending on the actions specified.
ASP.NET does take care of a lot of page events and such for you. as you have probably noticed by now, though, when you right click a text box and look at the available events, you only have a few, such as "textchanged". This is because anytime you do not actually submit a form to the server, you need AJAX to do a call to the server for you while not reloading the page. the "textchanged" event on a textbox is still going to be AJAX driven - it's just the Microsoft has built it in for you. You will want to look at either jQuery or the ASP.Net AJAX libraries.
You say you want to "store" the result - is it to generate new behavior later on the page? that's AJAX. Is it for longevity while the entire application is worked through? That can wait until the submit.
Actually the textChanged method waits for the Enter key to be hit.
I'm trying to learn about how AJAX stuff works under the hood. Say I have an asp.net form with a button on it. I then have a database with a single column DateTime. When I click the button on the asp.net form I want to insert a column in the database with the current time.
I'll have a C# event handler:
protected void btnButton_OnClick(Eventargs e, object sender)
{
//insert DateTime.now into DB
//This part is easy
}
What I would like to happen on the form itself is to have the button click NOT reload the form. It would just send a message to the server; there is no reason for the page to reload. What are some ways I could accomplish this?
I've looked into the AJAX framework a little bit and it seems like this could be done within an update panel, but the page would still reload, it would just be less noticable to the user.
Use the __doPostBack call in javascript. I have no idea how this works.
It sounds like you're partial to Microsoft's provided javascript libraries, which is fine. The UpdatePanel and the like have their place, but I've found that when you need to do something very simple and straightforward like what you're describing it is easier and cleaner to get it done with a direct call to a server resource from Javascript.
There are many ways to do this, but I like using jquery's $.ajax() method to invoke webservice methods (or MVC actions more recently). If you're willing to examine some slightly different technologies here's a blog post that will give you a taste of what I'm talking about.
ASP.NET Ajax has 2 major parts:
server side controls based
client side framework (which is called Microsoft Ajax)
With the first you would opt to make use of an UpdatePanel control. This however still sends a lot of unneeded data over the wire like the full viewstate, your pages follow almost the full page life cycle like with a normal postback. Mostly I recommend against using that in production code.
The second part is an ajax library based on pure javascript with a touch of Microsoft sauce over it. It has similarities with the page life cycle like a pageLoad function and such and is quite easy to learn.
__doPostBack
That function gets inserted when you place certain server controls on your page like a LinkButton control. You can call that from javascript directly but know that it'll cause a full page postback to the server so if you want to avoid that don't go that path.
During the last 2 years however I've become a big fan of jQuery which works quite well together with ASP.NET and ASP.NET MVC.
I suggest that you read this fine article to get more information about it: Using jQuery with ASP.NET Part 2: Making an AJAX Callback to ASP.NET.
jQuery itself has also been adopted and favored by Microsoft. I strongly suggest you take a look at it and its power.