views:

1826

answers:

5

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.

A: 

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.

JB King
A: 

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...

datacop
Thanks for your quick reply - I added a comment above to someone else's reply, so I'll just copy it again just in case you miss it: Please excuse the ignorance. We have had the MVC pattern here for over 10 years in a Windows-forms platform and just trying to move over to the web. Can you notice the learning curve? ;) I don't want to use aspx, so from the server-side, how do I load my userControls onto these darn things called aspx?! ;) Thanks!
WinForm controls cannot just be pasted onto a WebForm application and work. You have to rewrite the control.
rball
I know that rball. I have all the controls written on asp.net, and have created .ascx for robustness. Would like to dynamically load these controls via one page (aspx) using CreateChildControls() method that MS recommends in terms of design.
+1  A: 

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.

Groky
Thanks for your reply - like I noted above, we've had the MVC pattern in place for many years, and thus would like to hopefully use it as we move to the web? Is this possible or do we need to do a rewrite? Can't we just switch out the "V"? I know the web is stateless and will have to consider sessions, but any other issues to consider?
You're going to need to replace the V and C most likely. I couldn't imagine the controller syntax is the same as something from years ago. It would probably be similar though.
rball
Of course I have to replace the V as I noted already. I don't believe I have to replace the C, why do you think? I did a proof-of-concept for a small-scale project and in worked. Now I'm expanding the project to be more enterprise-wide and just asking a couple of questions.
I'm sorry there seems to be a misunderstanding. Are you using plain ASP.net or the ASP.net MVC framework? (see http://www.asp.net/mvc/)
Groky
+1  A: 

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.

Thanks for the suggestion. I'll definitely explore it!
A: 

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.

Chris Brandsma
I do not want to load the page inside another page. I want to load a modal form (subwindow/popup), where the underlying master page is still viewable while the user perfomrs edits on the sub-windows.Dropping controls via the designer is poor design. I want to to do this programmatically and thus dynamically, in which have many UserControls, via the MS recommended CreateChildControls() method.
Not all patterns are applicable in all environments. But...From the "Master page" you will have a link that loads your child page (location=_blank). that will load the next aspx page in a modal dialog.
Chris Brandsma