Tutorial :ASP.NET - How to load page via server-side code (C#) with MVC Pattern in place?


I am trying to use more server-side code (MVC pattern in place) and one of the methods that I am thinking of implementing is something like: LoadWebPage(). If a user is on a page, say Page1.aspx, and they click on a hyperlink, I would like to call the above method, LoadWebPage(). But the problem is, I do not know how to load another page as a subwindow (modal form), say Page2.aspx via server-side? Do I use Server.Transfer(), Response.Redirect(), or JavaScript (e.g. jQuery) to load the page? I want to persist session of course.

By the way, is there a way to call JavaScript via server-side code (C#)?

Any advice will be greatly appreciated.


It looks like you're trying to do MVC with the standard ASP.net WebForms framework. It's probably a bit late for you now, but have you taken a look at the ASP.net MVC framework? That might make you life a lot easier.


I've been using ASP.NET MVC for a while and I use jquery for doing what you are doing. What you would do is create another view which will be retrieved by jquery using an ajax call. In your case for example, I would have a Controller named AjaxController with an action LoadWebPage (and of course, the corresponding view LoadWebPage.aspx). Jquery would simple call the URL:

$.get('<%= Url.Action("LoadWebPage", "Ajax") %>', function(data) { $('#mydiv').html(data); // here we load the contents of the page to a div with an id of 'mydiv' });

Basically, your approach should be the other way around. Use the client via jquery to load the page and not the other way around.


I'm thinking you may have some models mixed up here. If you want an event fired after the page has loaded this is usually called a Post Back and there are some specific ways to get that event fired off in ASP.Net if you look at say the Button control and its "On_Click" event.

To route the user to another page, it depends on what sequence of events do you want to have happen:

1) Request merely shifts over to Page2.aspx for loading. In this case, Server.Transfer is fine as long as you recognize that the client isn't going to know that Page2 is the current URL as they requested Page1.aspx.

2) Route the user to a different page, updating the browser's history. Here you use a Response.Redirect to send the client over to Page2.aspx and can set whatever querystring parameters you need to get the job done.

3) AJAX-handling. If you don't want to do a post back, there are ways to use AJAX to handle calling a server side function withou having the user leave the page. This may be the trickiest to do and is the most slick however.


Umm... Page1.aspx, Page2.aspx.. that's not MVC.. you're working with the webforms model.

As far as opening up a "sub" page in a modal window, there's dozens of examples.. You could be all "slick" and use ASP.Net AJAX and the AjaxControlToolkit

You could use a 3rd party commercial control from a vendor like Telerik

You could use jQuery and load the popup modal form in an iFrame using standard get requests...


If you are not going to use the MVC Framework (http://www.asp.net/mvc) I would use the MVP model instead. WebForms by themselves are not MVC friendly.

Now, if you want to load a page inside of another page you use an IFrame to do that.

If you want to load a user control onto you page, you can actually just drop the control from the toolbox onto your form.

But you really should get a web development book, or go thru the samples on the asp.net web site.

EDIT: it is not easy to call JavaScript from the server side. But you can use JavaScript to call a web service which could be on your server. The JavaScript could run as the page loads.

EDIT 2: Another item that I have seen confuse desktop developers: how to navigate. And I think that might be giving you some trouble here as well.

The link, all by itself will tell the browser to redirect to another page. You don't want to have to go back to the server for that. It isn't needed. The link needs another attribute: target="_blank" and the next page will appear in a popup dialog. You pass data to the popup via query string parameters. Else you can "save" data into session as well.

Note: the entire browser popup model is broken. Between tabbed browsers and popup blockers you will have trouble. Best if you can use normal browser navigate and drop the entire popup idea entirely. UI paradigms you use on desktop do not always work on web.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »