题
我开始失去一些完全平庸的事情了我的神经:我不从一个TextBox获取用户输入:S
我不喜欢这样(后面的aspx代码):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
}
this._presenter.OnViewLoaded();
txtBox1.Text = "blah";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do sth with txtBox1.Text but when I read it, it is still the same as when a loaded the page at Page_Load, So if I entered "blahblah" in the txtBox1 via browser the text I get when I debug or run is still "blah"
}
和在ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertStudent.aspx.cs" Inherits="IzPT.Vb.Views.InsertStudent"
Title="VnosProfesorja" MasterPageFile="~/Shared/DefaultMaster.master" %>
<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" Runat="Server">
<h1>Student</h1>
<p>
<table style="width:100%;">
<tr>
<td style="width: 139px">
Name</td>
<td>
<asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
</p>
</asp:Content>
我也试图与的DetailsView做到这一点,将其绑定到一个列表中,但是当我在编辑模式下读出的值I有同样的问题。
任何想法?
解决方案
您会在每个Page_Load中设置文本框Text属性为“胡说”。由于ViewState中已经在这一点上加载,你重写任何值的用户输入。
如果您只想将Text值设置一个时间,然后确保你把它if (!IsPostBack)
检查里面。
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
txtBox1.Text = "blah";
}
this._presenter.OnViewLoaded();
}
其他提示
您的问题是,你是在Page_Load中不断变化的价值!
Page_Load
被Button1_Click
之前运行。
移动代码的Page_Load此
protected override void OnLoadComplete(EventArgs e)
{
txtBox1.Text = "blah";
}
或保护你的代码......像这样
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
}
的Page_Load被后背部被重置在文本框中的值时调用。变化到
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
this._presenter.OnViewInitialized();
}
我个人有视图中的属性来设置从呈现的文本框的值。在OnViewInitialized()或OnViewLoaded()。
不隶属于 StackOverflow