文本框在Asp.net使用视图状态preserve价值? [英] Preserve value of textbox using View State in Asp.net?
本文介绍了文本框在Asp.net使用视图状态preserve价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮和一个文本框。我想在文本框中输入一个值,当我点击按钮的页面将重新加载但该值应该还是在文本框中。我怎样才能做到这一点。下面code不起作用
命名空间WebApplication2
{
公共部分类_Default:System.Web.UI.Page
{ 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!Page.IsPostBack)
{
如果(的ViewState [值]!= NULL)
{
TextBox1.Text =的ViewState [值]的ToString()。
}
}
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
的ViewState [值] = TextBox1.Text;
的Response.Redirect(Default.aspx的);
}
}
}
解决方案
视图状态可以只保留直到当你在同一页上的价值。您将会重定向到其它页面。因此,而不是使用ViewState的使用session。
I've a button and a textbox. I want a value to be entered in textbox and when I click on button the page will reload but the value should still be in the textbox. How can I do that. The following code doesn't work
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = ViewState["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}
解决方案
Viewstate can retain the value only till when you are on the same page. You are redirecting to other page. So instead of using viewstate use session.
这篇关于文本框在Asp.net使用视图状态preserve价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文