ASP.NET MVC and ModalPopupExtender - c#

I am developing an application with ASP.NET MVC and wish to make use of Modal style windows.
Here is one example. On my "Login" view, I have my login details, but also have a button to create an Account for the application. I have a separate "CreateAccount" view, but would like it to pop up inside a ModalPopupWindow.
Is this possible usng just ASP MVC and the ASP.NET Ajax Library, or will I have to use a different Ajax library?
(Another way to ask this question: can I redirect the output of a view into a ModalPopupExtender?)
Jason

I would suggest you returning a PartialView from your controller and loading the result in some jQuery plugin to show popups in the same page context like this
The usage is something like
Boxy.load('/Account/Create'); // You can post additional data if you need to
This way you will have a beautiful Box (facebook like) with your form in it. You just have to be aware that if you post something in it, you should do it using AJAX to update the response in the same Box.
Good Luck!!

Related

How is button connected in asp.net MVC app?

I have an asp.net MVC app. There is a <button> in a .cshtml file. The button only has an id. There isn't any javascript associated with it. I don't see this button name in any of the controllers.
Yet, when the button is clicked, it doesn't perform an action. How do I trace down what is triggering the button action once it is clicked?
I think you are a little confused on how ASP.NET WebForm and MVC works.
While WebForm is very much similar to WinForms , in a sense that controls can have events that gives it a sense of Stateful appearance, MVC is not the same, it embraces the stateless nature of HTTP.
In MVC when a button is clicked, it is usually associated with a controllerand a action to which it posts the data. So, a button click is translated to action on a control same like method invocation in a WebForm.
Q. The controller still needs to know about the button to handle the click?
there is no existence of control on server side, its the downside (+side from my view) of being stateless. In MVC there is no click, but only method invocation on the controllers.
A same action can be invoked using 10 different buttons. If u can share the code of <view>.cshtml then may be I can help a little bit more.
There could be java script in a separate file. In that JavaScript file there must be click handler attached that must be calling your action in controller via whatever mechanism.
To begin with .. Search in your project where else that button id is used.
That could be the point where you can start tracing...
By virtue of the button being defined within the Html.BeginForm braces, the button click is wired to perform a POST submission with the form data.

How to call a C# method from asp.net side?

This seems to be a common question but I could not find a relevant answer
I have a aspx page and in it,
I've made a list of "a href" tags that is generated from my C# code behind.
Now i want to send data based on which of the "a href" tag i have selected and pass it into my C# code which will then populate my popup with required information:
The process
"a href click" -> call c# method -> populate popup with data
I am stuck at how to pass data from the "a href click" to the c#.
I tried using .OnServerClick but my popup didn't even pop up.
For starters, i would like to try: When i Click a "a href" tag it would call my changeTitle() method from c# which will change the title of the pop up. If i can get this working, i should be able to manage the rest
c# method:
public void changeTitle()
{
this.modalTitle.InnerHtml = "Goodbye world";
}
Do tell me if you need more information please, I really hope to get this working
You should really look into learning more about how jQuery and ASP.NET can work together. I highly recommend starting at Dave Ward's site.
Here is a blog entry about using jQuery to call ASP.NET server methods (page methods are a great way to get quick hooks into server-side logic that can pass data back to the client-side):
Using jQuery To Directly Call ASP.NET AJAX Page Methods
I have used this technique in many projects over the years and I think once you start learning the power of jQuery you will want to use this approach over strictly server-side controls.
If ASP.NET WebForms is being used (remember to specify which "flavor" of ASP.NET), a LinkButton control may be a suitable approach1.
A LinkButton works like a a normal Button that looks like a hyperlink and causes a PostBack when clicked. (This is different from "normal" hyperlinks that change the browser location.)
The OnClick attrtibute specifies the server callback handler and the Command and CommandArguments can be used to associate specific data with the control that are available on the server during the LinkButton's click callback.
1 While my current preferred form of development is a thick client with a thin backend (e.g. not WebForms), switching to use "Page Methods" or "AJAX" requires rewriting the front-end/HTML to act on the response as appropriate. A LinkButton on the other hand, simply "works" with the normal ASP.NET WebForms lifecycle/infrastructure without any additional work.

Sharing view presentation mode?

I am building an ASP .NET MVC 3 application, and I want to make sure that I am following the MVC guidelines as well as possible.
I have a front page that displays a list of computer games. Now, it has the same way of displaying the items as another page. How would I do this through an MVC perspective?
Would I have a function in my controller that returns a string containing the HTML of the list of games, and then print that out on both pages? Or how would I do this in a proper way?
partial views. you should be able to find a lot of information about them via search.

jQuery 'viewstate'-like behavior

I'm building a website in jquery mobile. It is a SOA application and on 'pageshow' event I call the web services get the data and populate labels and dropdown lists with it. However, say for instance, when a user clicks back and the app takes him back to dashboard, the ajax call is made again and the labels are unnecessarily populated again. What I want to ask is, can I prevent this behaviour of populating the same labels with the same data over and over again? Does jquery mobile have this 'viewstate' behavior built in?
Thank you for taking the time to answer my question.
You could use HTML5's localStorage / sessionStorage API to actually persist data between callbacks.
Personally I think that if you'd use JQM's page idiom (having only data-role=page and not loading any new ajax pages), you wouldn't have this problem at all (but rather the opposite, how to reset all fields).

Form POST in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5. The page has several forms on it for several different things.
In ASP.NET, there's a server form control wrapping the entire page, and form controls don't work within a server form control, and you can't have more than one server form control on a page.
So in order to keep this functionality, I can either:
Remove the server form control that's wrapping the page, and leaving the html forms on the page.
Create button click events for every form and perform the POST in the code-behind.
What's the preferred method here?
I wonder if converting to vanilla asp.net (aka webforms) is a bad idea here. Personally I'd go to MVC instead - allows multiple forms etc, and the views are much closer to he HTML, a lot like ASP.
I guess I'm saying there are some glitches vanilla asp.net introduces that you don't have to suffer.
I would go with the second option, any button click is going to post the whole page back anyway so you're not saving any bandwidth. Simply handle each button appropriately.
Check the answer I provided to a similar question here :-)
How to get past embedding a html form for paypal buttons asp.net
If you're going to use different button clicks, you still need to use this override to disable the non-related buttons in each handler, otherwise it won't work. You can only have one form tag at a time - this way you can toggle/disable the ones you're not using as appropriate.
Better still, refactor your application to use a single form. While MVC would be a closer match to the model you're using right now, it wouldn't make sense to go that route unless you were experienced enough with it; Web Forms is an easier jump.

Categories