ASP.NET - 如何通过服务器端代码(C#)加载MVC模式的页面?

时间:2021-10-23 15:12:57

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.

我试图使用更多的服务器端代码(MVC模式到位),我正在考虑实现的方法之一是:LoadWebPage()。如果用户在页面上,比如Page1.aspx,并且他们点击超链接,我想调用上面的方法,LoadWebPage()。但问题是,我不知道如何加载另一个页面作为子窗口(模态形式),通过服务器端说Page2.aspx?我是否使用Server.Transfer(),Response.Redirect()或JavaScript(例如jQuery)来加载页面?我当然希望坚持会议。

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

顺便问一下,有没有办法通过服务器端代码(C#)调用JavaScript?

Any advice will be greatly appreciated.

任何建议将不胜感激。

5 个解决方案

#1


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.

看起来你正在尝试使用标准的ASP.net WebForms框架进行MVC。你现在可能有点晚了,但是你看过ASP.net MVC框架了吗?这可能会让你的生活变得更轻松。

#2


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:

我已经使用ASP.NET MVC一段时间了,我使用jquery做你正在做的事情。你要做的是创建另一个视图,它将由jquery使用ajax调用来检索。在你的例子中,我将有一个名为AjaxController的Controller,其中包含一个动作LoadWebPage(当然还有相应的视图LoadWebPage.aspx)。 Jquery会简单地调用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' });

$ .get('<%= Url.Action(“LoadWebPage”,“Ajax”)%>',function(data){$('#mydiv')。html(data); //这里我们加载的内容id为'mydiv'}的div的页面;);

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

基本上,你的方法应该是相反的方式。通过jquery使用客户端加载页面而不是反过来。

#3


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.

我想你可能会在这里混淆一​​些模特。如果你想在页面加载后触发一个事件,这通常被称为Post Post,如果你看一下Button控件及其“On_Click”事件,有一些特定的方法可以在ASP.Net中触发该事件。

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.

1)请求仅转移到Page2.aspx进行加载。在这种情况下,只要您认识到客户端在请求Page1.aspx时不知道Page2是当前URL,Server.Transfer就可以了。

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.

2)将用户路由到不同的页面,更新浏览器的历史记录。在这里,您使用Response.Redirect将客户端发送到Page2.aspx,并可以设置完成工作所需的任何查询字符串参数。

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.

3)AJAX处理。如果您不想回发帖子,可以使用AJAX来处理调用服务器端功能而不让用户离开页面。这可能是最棘手的事情,但是最光滑。

#4


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

嗯... Page1.aspx,Page2.aspx ..那不是MVC ..你正在使用webforms模型。

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

至于在模态窗口中打开一个“子”页面,有几十个例子..你可以全部“光滑”并使用ASP.Net AJAX和AjaxControlToolkit

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

您可以使用来自Telerik等供应商的第三方商业控制

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

您可以使用jQuery并使用标准的get请求在iFrame中加载弹出模式窗体...

#5


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.

如果您不打算使用MVC框架(http://www.asp.net/mvc),我会使用MVP模型。 WebForms本身并不是MVC友好的。

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

现在,如果您想在另一个页面中加载页面,可以使用IFrame来执行此操作。

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.

但你真的应该得到一本网络开发书,或者通过asp.net网站上的样本。

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.

编辑:从服务器端调用JavaScript并不容易。但您可以使用JavaScript来调用可能在您的服务器上的Web服务。 JavaScript可以在页面加载时运行。

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.

编辑2:我看到的另一个项目混淆了桌面开发人员:如何导航。而且我认为这也可能给你带来一些麻烦。

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.

该链接本身将告诉浏览器重定向到另一个页面。您不希望必须返回服务器。它不是必需的。该链接需要另一个属性:target =“_ blank”,下一页将出现在弹出对话框中。您通过查询字符串参数将数据传递给弹出窗口。否则,您也可以将数据“保存”到会话中。

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.

注意:整个浏览器弹出模型已损坏。在选项卡式浏览器和弹出窗口阻止程序之间,您将遇到麻烦。最好的,如果你可以使用普通的浏览器导航并完全放弃整个弹出的想法。您在桌面上使用的UI范例并不总是适用于Web。

#1


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.

看起来你正在尝试使用标准的ASP.net WebForms框架进行MVC。你现在可能有点晚了,但是你看过ASP.net MVC框架了吗?这可能会让你的生活变得更轻松。

#2


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:

我已经使用ASP.NET MVC一段时间了,我使用jquery做你正在做的事情。你要做的是创建另一个视图,它将由jquery使用ajax调用来检索。在你的例子中,我将有一个名为AjaxController的Controller,其中包含一个动作LoadWebPage(当然还有相应的视图LoadWebPage.aspx)。 Jquery会简单地调用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' });

$ .get('<%= Url.Action(“LoadWebPage”,“Ajax”)%>',function(data){$('#mydiv')。html(data); //这里我们加载的内容id为'mydiv'}的div的页面;);

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

基本上,你的方法应该是相反的方式。通过jquery使用客户端加载页面而不是反过来。

#3


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.

我想你可能会在这里混淆一​​些模特。如果你想在页面加载后触发一个事件,这通常被称为Post Post,如果你看一下Button控件及其“On_Click”事件,有一些特定的方法可以在ASP.Net中触发该事件。

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.

1)请求仅转移到Page2.aspx进行加载。在这种情况下,只要您认识到客户端在请求Page1.aspx时不知道Page2是当前URL,Server.Transfer就可以了。

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.

2)将用户路由到不同的页面,更新浏览器的历史记录。在这里,您使用Response.Redirect将客户端发送到Page2.aspx,并可以设置完成工作所需的任何查询字符串参数。

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.

3)AJAX处理。如果您不想回发帖子,可以使用AJAX来处理调用服务器端功能而不让用户离开页面。这可能是最棘手的事情,但是最光滑。

#4


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

嗯... Page1.aspx,Page2.aspx ..那不是MVC ..你正在使用webforms模型。

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

至于在模态窗口中打开一个“子”页面,有几十个例子..你可以全部“光滑”并使用ASP.Net AJAX和AjaxControlToolkit

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

您可以使用来自Telerik等供应商的第三方商业控制

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

您可以使用jQuery并使用标准的get请求在iFrame中加载弹出模式窗体...

#5


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.

如果您不打算使用MVC框架(http://www.asp.net/mvc),我会使用MVP模型。 WebForms本身并不是MVC友好的。

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

现在,如果您想在另一个页面中加载页面,可以使用IFrame来执行此操作。

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.

但你真的应该得到一本网络开发书,或者通过asp.net网站上的样本。

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.

编辑:从服务器端调用JavaScript并不容易。但您可以使用JavaScript来调用可能在您的服务器上的Web服务。 JavaScript可以在页面加载时运行。

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.

编辑2:我看到的另一个项目混淆了桌面开发人员:如何导航。而且我认为这也可能给你带来一些麻烦。

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.

该链接本身将告诉浏览器重定向到另一个页面。您不希望必须返回服务器。它不是必需的。该链接需要另一个属性:target =“_ blank”,下一页将出现在弹出对话框中。您通过查询字符串参数将数据传递给弹出窗口。否则,您也可以将数据“保存”到会话中。

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.

注意:整个浏览器弹出模型已损坏。在选项卡式浏览器和弹出窗口阻止程序之间,您将遇到麻烦。最好的,如果你可以使用普通的浏览器导航并完全放弃整个弹出的想法。您在桌面上使用的UI范例并不总是适用于Web。