Asp.net页面传值的几种方式
一、使用QueryString参数
QueryString将传递的值显示在浏览器的地址栏中,是一种非常简单也使用比较多的传值方式。
如果传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法
/// <summary>
/// 使用QueryString变量
/// </summary>
/// <param name="param1"></param>
/// <param name="param2"></param>
//sourcePage
Response.Redirect("target.aspx?param1 = hello¶m2 = hi");
//targetPage
string str1 = Request.QueryString["param1"];
string str2 = Request.QueryString["param2"];
二、使用cookie对象变量
cookie存放在客户端中
//setting cookie
HttpCookie cookie_name = new HttpCookie("name");
cookie_name.value = txtName.Text.Trim();
Response.AppendCookie (cookie_name);
//get cookie
string name = Request.Cookie["name"].Value.ToString();
三、使用session变量
session存放在服务器中,如果连接超时等情况,有可能会发生数据丢失
//setting session
Session["name"] = "hello";
//get session
string name = Session["name"].ToString();
四、使用Application变量
Application变量的作用范围是整个全局,也就是说对所有的用户都有效,因此此种方法不常用。
因为Application在一个应用程序域范围内共享,所有用户都可以改变或者设置其值,故只应用计数器等需要全局变量的地方
//setting Application
Application["name"] == "hello";
//get Application
string name = Application["name"].ToString();
五、使用PostBackUrl()
//sourcePage default1.aspx
<asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/default2.aspx">
</asp>
//targetPage default2.cs
if (PreviousPage != null)
{
TextBox tb = (TextBox)Previous.FindControl("TextBox1");
string str = tb.Text;
}
六、使用Server.Transfer方法
这个才可以说是面向对象开发所使用的方法
使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法完全面向对象的,简洁有效。
下面这个代码是展示在需要很多个参数的时候使用的方法,如果参数比较少就没有必要使用这个方法了
如果让所有的查
相关文档:
1. 当在一个 Action 中 对相同功能的2个控件进行赋值
List<SelectListItem> WebSite = GetWebSite();
ViewData["SiteNames"] = WebSite;
if (WebSite.Count > 0)
WebSite.RemoveAt(0);
ViewData["SiteName"] = WebSite;
页面显示时会统一以 WebSite ......
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace TLibrary.ObjectHelper
{
public class CookiesHelper
{
#region ......
今早有个网友问到我这问题,以前我都是直接在数据库中存文件名的,还没有试过存储整张图片到数据库中,上网搜索了一下,自己又测试了一番,代码如下:
建立保存图片的表的SQL语句:
Sql代码 < width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://w ......
今早有个网友问到我这问题,以前我都是直接在数据库中存文件名的,还没有试过存储整张图片到数据库中,上网搜索了一下,自己又测试了一番,代码如下:
建立保存图片的表的SQL语句:
Sql代码 < width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://w ......
以下是系统自动生成的回调函数
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.ons ......