Ajax与ViewState
在Ajax方法中可以设置Session,但因为页面关闭了,Session还在,且又不能清空Session所以,选择使用ViewState,但不能将ViewState带过来,为何? --------------------编程问答-------------------- viewstate是服务器变量,js取不到,所以需要一个服务器端函数
[AjaxMethod]
public string GetViewStateValue(string key)
{
if(ViewState[key]==null)
{
return "";
}
return ViewState[key].ToString();
}
然后用ajax访问 --------------------编程问答-------------------- 验证可以访问到ViewState
Atlas下的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language"javascript" type="text/javascript">
function Test()
{
PageMethods.GetViewState('n2dog',onCompleted);
}
function onCompleted(result)
{
$('Text1').value=result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<input id="Button1" type="button" value="button" onclick="Test()" /><input id="Text1"
type="text" />
</div>
</form>
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
</components>
</page>
</script>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ViewState["n2dog"] = "123";
}
[WebMethod]
public string GetViewState(string key)
{
if (ViewState[key] == null)
{
return "";
}
return ViewState[key].ToString();
}
} --------------------编程问答-------------------- ASP.NET ViewState 初探
http://www.microsoft.com/china/msdn/archives/library/dnaspnet/html/Asp11222001.asp
希望提供的信息对你有帮助! --------------------编程问答-------------------- [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void Test()
{
ViewState["test"] = "Test";
}
在Page_Load()中
Response.Write(ViewState["test"].ToString());
失败了! --------------------编程问答-------------------- 还没解决 --------------------编程问答-------------------- 肯定不可以的,ViewState只有在页面回传后才会生成.ajax不会引发页面回传.在客户端ViewState表现为隐藏的input标签, 页面没有回传, 生成不了隐藏input,怎么可以获得呢.
AJAX就是局部更新, 如果更新ViewState,又会有大量的页面内容被更新, 不符合AJAX的设计初衷.建议使用UpdatePanel
补充:.NET技术 , C#