asp.net(c#)网页跳转方法汇总

本文详细介绍了ASP.NET中的七种页面跳转方法,包括Response.Redirect、Server.Transfer及Server.Execute等,对比了它们的特点与应用场景,帮助开发者根据不同需求选择合适的跳转方式。

asp.net(c#)网页跳转七种方法

1.Response.Redirect("https://blog.youkuaiyun.com/YearnYea/article/details/79764760",false); 

目标页面和原页面可以在2个服务器上,可输入网址或相对路径。后面的bool值为是否停止执行当前页。 
跳转向新的页面,原窗口被代替。" 
浏览器中的URL为新路径。 
:Response.Redirect方法导致浏览器链接到一个指定的URL。当Response.Redirect()方法被调用时,它会创建一个应答,应答头中指出了 
状态代码302(表示目标已经改变)以及新的目标URL。浏览器从服务器收到该应答,利用应答头中的信息发出一个对新URL的请求。这就是说, 
使用Response.Redirect方法时重定向操作发生在客户端,总共涉及到两次与服务器的通信(两个来回):第一次是对原始页面的请求, 

得到一个302应答,第二次是请求302应答中声明的新页面,得到重定向之后的页面。 


2.Server.Transfer("Default.aspx?name=zhangsan",true); 
目标页面和原页面可以在同一个服务器上。 
跳转向新的页面,原窗口被代替。 
波球论坛 浏览器中的URL为原路径不变。 
默认情况下,Server.Transfer方法不会把表单数据或查询字符串从一个页面传递到另一个页面,但只要把该方法的第二个参数设置成 
Tb310True,就可以保留第一个页面的表单数据和查询字符串。 
同时,使用Server.Transfer时应注意一点:目标页面将使用原始页面创建的应答流(Machine Authentication Check,MAC)认为新页面的ViewState已被篡改。因此,如果要保留原始页面的表单数据和查询字符串集合, 

必须把目标页面Page指令的EnableViewStateMac属性设置成False。 


3.Server.Execute("Default5.aspx?address=beijing); 
目标页面和原页面可以在同一个服务器上。 
跳转向新的页面,再跳转会原页面。 
浏览器中的URL为原路径不变。 
当指定的ASPX页面执行完毕,控制流程重新返回原页面发出Server.Execute调用的位置。 
这种页面导航方式类似于针对ASPX页面的一次函数调用,被调用的页面能够访问发出调用页面的表单数据和查询字符串集合,所以要把 

被调用页面Page指令的EnableViewStateMac属性设置成False。 


4.Response.Write("<script language='javascript'>window.open('aaa.aspx');</script>");_ 
目标页面和原页面可以在2个服务器上,可输入网址或相对路径。 

原窗口保留,另外新增一个新页面。 


5.Response.Write("<script language='javascript'>window.location='Default2.aspx'</script>"); 

打开新的页面,原窗口被代替。 


6.Response.Write("<script>window.showModalDialog('Default2.aspx')</script>"); 


7.Response.Write("<script>window.showModelessDialog('Default2.aspx')</script>");


三种跳转方式:response.redirect,server.transfer,sever.execute解析


  1、response.redirect:这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳  转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!
  redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session。


  顺便提一下,如何使用redirect方法在查询字符串中使用汉字,因为经常的情况是出现乱码,原因是url不支持汉字。这个时候需要转换:
  string message =server.urlencode("欢迎来到微尘里博客");
先转换,在使用查询字符串:
  response.redirect("webform2.aspx?msg="+message);

  2、server.transfer:速度快,只需要一次postback ,但是……他必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。
  你可以写个小程序试试:设计一个由页面一到页面二的跳转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!


  3、sever.execute:这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。



asp.net中有四种页面跳转导航方式,如何选择?

  ◆ 如果要让用户来决定何时转换页面以及转到哪一个页面,超级链接最适合。

  如果要用程序来控制转换的目标,但转换的时机由用户决定,使用Web服务器的HyperLink控件,动态设置其NavigateUrl属性。

  ◆ 如果要把用户连接到另一台服务器上的资源,使用Response.Redirect。

  用Response.Redirect把用户连接到非ASPX的资源,例如HTML页面。

  如果要将查询字符串作为URL的一部分保留,使用Response.Redirect。

  ◆ 如果要将执行流程转入同一Web服务器的另一个ASPX页面,应当使用Server.Transfer而不是Response.Redirect,因为Server.Transfer能够避免不必要的网络通信,从而获得更好的性能和浏览效果。

  ◆ 如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。

  注:如果要确保HTML输出合法,请使用Response.Redirect,不要使用Server.Transfer或Server.Execute方法。



关于Server.Execute


  这种页面导航方式类似于针对ASPX页面的一次函数调用,被调用的页面能够访问发出调用页面的表单数据和查询字符串集合,所以要把被调用页面Page指令的EnableViewStateMac属性设置成False。


  默认情况下,被调用页面的输出追加到当前应答流。但是,Server.Execute方法有一个重载的方法,允许通过一个TextWriter对象(或者它的子对象,例如StringWriter对象)获取被调用页面的输出,而不是直接追加到输出流,这样,在原始页面中可以方便地调整被调用页面输出结果的位置。


asp.net实现从一个页面跳转到另一页面方法的比较


  实现页面跳转的方法很多,除了服务器控件是asp.net封装过外,其它在asp中也就有了。


  这么多实现方法中,都是实现同样的功能,为什么还要支持如此多的方法呢,我们实际应用中到底应该选择哪一种方法呢?也许我们大多数人都是需要用跳转时,随便想到一个便拿来用,从不去考虑它们会有不同,其实之前我也是这样的,对他们没有一个系统的了解是不能发挥它们各自的优势的。好,现在让我们来了解一下它们各自的用途吧。

  1、超链接

  2、服务器控件 HyperLink

  通过NavigateUrl属性来指定要跳转的url。主要用于由用户来决定何时转换页面,但是用程序来控制转换的目标。

  3、程序控制

  ① Response.Redirect() 
  服务器--------》通知客户端----------》请求跳转到服务器端
  主要用于需要将查询字符串作为url的一部分传递给另一页面,或跳转的页面是html页面

  ② Server.Transfer() 
  使用此方法最大的特点是url仍然是跳转前的页面的url。 
  主要用于需要将执行流程转到同一web服务器的另一aspx页面时用。因为它在跳转后保留了request和session值,并且可以使用跳转前页面的数据。

  ③ Server.Execute() 
  此方法执行完后程序控制权会返回到跳转代码的下一代码,相当于我们的模态窗口,此时可以得到跳转过页面的返回值。

  常用于辅助选择操作,然后返回选择后的值到当前页面。


详细出处参考百度,太多了不知道原作者是哪位。

Lab 2 Chapter 1 Java Language Programming Chapter 1: Variables and Primitive Data Types (Part 2) Instructor: Ahsan Shehzad Date: September 3, 2025 Practical Session Objective Lab Goal: Store and Display a Contact Our objective is to create a simple Java program that stores the information for one contact using variables and then prints that information neatly to the console. Final Result Preview: (This is what your console output will look like) (Screenshot of the final console output will go here) Prerequisites: Java Development Kit (JDK) 11 or higher installed. An IDE like IntelliJ IDEA Community Edition ready to go. Step-by-Step Guided Exercise Step 1: Create the Project Structure Goal: Set up your project in IntelliJ and create the main class file. Instructions: 1. Open IntelliJ IDEA. 2. Go to File > New > Project... . 3. Name your project MyContactManager and choose a location to save it. Contact Profile: Name: John Doe Age: 32 Phone: 447700900123 Is a Friend: true 1 2 3 4 54. Ensure Java is selected and you have a JDK configured. 5. Once the project is created, right-click the src folder in the Project Explorer. 6. Select New > Java Class . 7. Name the class Main and press Enter. Code Block: Your IDE will generate this boilerplate code for you. Add the main method inside the class. Expected Result: You have a clean Main.java file, and the program can be run (though it won't do anything yet). Step 2: Declare and Initialize Contact Variables Goal: Create variables inside the main method to hold a contact's data. Instructions: 1. Inside the main method, declare and initialize variables for each piece of contact information. 2. Choose the most appropriate data type for each value. 3. Pay special attention to the literals for long ( L ) and String (double quotes). Code Block: Add the following lines inside your main method. Expected Result: The program should compile without any errors. When you run it, still nothing will happen, but the data is now stored in memory. public class Main { public static void main(String[] args) { // Our code will go here! } } 1 2 3 4 5 public static void main(String[] args) { // String is a special object type we use for text. String firstName = "John"; String lastName = "Doe"; // Use 'int' for age. int age = 32; // A phone number can be large, so 'long' is a safe choice. long phoneNumber = 447700900123L; // 'boolean' is perfect for a simple true/false status. boolean isFriend = true; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14Step 3: Display the Information Goal: Use System.out.println() and string concatenation ( + ) to print the stored data to the console. Instructions: 1. After the variable declarations, add a println statement to act as a header. 2. For each variable, write a println that combines a descriptive label (e.g., "Name: ") with the variable itself using the + operator. Code Block: Add these lines after your variables in the main method. Expected Result: When you run the main method, you should see the formatted contact details printed to your console, matching the goal on slide 13. Putting It All Together Goal: Review the complete, final code for this lab session. Instructions: Your final Main.java file should look exactly like this. Make sure your code matches and run it one last time to confirm the output. Code Block: // ... variables from previous slide ... // --- Displaying the Information --- System.out.println("Contact Profile:"); System.out.println("Name: " + firstName + " " + lastName); System.out.println("Age: " + age); System.out.println("Phone: " + phoneNumber); System.out.println("Is a Friend: " + isFriend); 1 2 3 4 5 6 7 8 public class Main { public static void main(String[] args) { // --- Storing Contact Information --- // String is a special object type we use for text. String firstName = "John"; String lastName = "Doe"; // Use 'int' for age. int age = 32; // A phone number can be large, so 'long' is a safe choice. long phoneNumber = 447700900123L; // 'boolean' is perfect for a simple true/false status. boolean isFriend = true; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Expected Result: A clean, working program that successfully stores and displays data. You've just completed Phase 1 of the project! (This slide is intentionally left blank to fit the 21-slide structure. It can be used for instructor-specific notes or an extra exercise if needed.) Challenge Task For Those Who Finish Early... Challenge 1: Add a Second Contact Declare and initialize a new set of variables for a second person (e.g., firstName2 , age2 , etc.). Print their details to the console, separated from the first contact by a line of dashes ( "----------" ). Challenge 2: Perform a Calculation After creating both contacts, declare a new double variable named averageAge . Calculate the average age of the two contacts and store it in the variable. Hint: (age1 + age2) / 2.0 . Why / 2.0 and not / 2 ? Print the average age to the console with a descriptive label. // --- Displaying the Information --- System.out.println("Contact Profile:"); System.out.println("Name: " + firstName + " " + lastName); System.out.println("Age: " + age); System.out.println("Phone: " + phoneNumber); System.out.println("Is a Friend: " + isFriend); } } 17 18 19 20 21 22 23 24 答案是什么
最新发布
09-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值