保存的值,并从视图状态读 [英] Saving a value to and reading from the viewstate

查看:172
本文介绍了保存的值,并从视图状态读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是太熟悉.NET,但我要救一个简单的值(1和1000之间的数字,这是一个特殊的高度 DIV )的视图状态和检索它时,更新面板重新加载(无论是在某处标记或使用JavaScript)。什么是最简单的方法是什么?

I'm not too familiar with .NET, but I want to save a simple value (a number between 1 and 1000, which is the height of a particular div) to the viewstate and retrieve it when the update panel reloads (either in the markup somewhere or with javascript). What is the simplest way to do this?

此页面给我以下code:

string strColor;
if (Page.IsPostBack)
{
   // Retrieve and display the property value.
   strColor = (string)ViewState["color"];
   Response.Write(strColor);
}
else
   // Save the property value.
   ViewState["color"] = "yellow";

不过,我不是在哪里或如何访问例如strColor完全清楚。

However, I'm not totally clear on where or how to access the example strColor.

由于这是在code的后面,在那里将的Response.Write 吐甚至是code呢?我找不到它,当我尝试这个code。我如何使用JavaScript来设置值,而不是背后的code设置呢?

Since this is in the code behind, where will Response.Write even spit that code out? I couldn't find it when I tried this code. And how do I use javascript to set that value, instead of setting it in the code behind?

推荐答案

您可以简单地设置分区作为服务器控件像这样:

You can simply set the div as a server control as so:

<div id="yourdiv" runat="server" ...

而当页面回;只需设置它通过设置其属性的高度;例如:

And when the page posts back; simply set it's height by setting its attributes; for example:

yourDiv.Attributes("style","height:"+height_read_from_ViewState+"px;");

或者,你可以存储在客户端的高度,采用了隐藏字段,并读取隐藏字段的值在服务器端设置div的高度。

Or, you can store the height on the client side, using a Hidden field and reading that hidden field's value on the server side to set the div's height.

<asp:hiddenfield id="hdnHeight" runat="server" />

您在JavaScript设置的高度像这样:

You set the height in Javascript as so:

function setHeight(value)
{
  document.getElementById('<%=hdnHeight.ClientID').value=value;
}

和上回发服务器端:

yourDiv.Attributes("style","height:"+hdnHeight.Value+"px;");

这篇关于保存的值,并从视图状态读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆