2

我开始对一些完全平庸的事情感到不安:我没有从 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 执行此操作并将其绑定到列表,但是当我在编辑模式下读取值时,我遇到了同样的问题。

有任何想法吗?

4

4 回答 4

5

您在每个 Page_Load 上将文本框 Text 属性设置为“blah”。由于此时已经加载了 ViewState,因此您将覆盖用户输入的任何值。

如果您只想设置一次 Text 值,请确保将其放入if (!IsPostBack)检查中。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._presenter.OnViewInitialized();
            txtBox1.Text = "blah";
        }
        this._presenter.OnViewLoaded();

    }
于 2009-12-29T16:57:32.770 回答
2

您的问题是您正在更改 Page_Load 中的值!

Page_Load之前运行Button1_Click

将代码从 Page_Load 移至此

protected override void OnLoadComplete(EventArgs e)
{
    txtBox1.Text = "blah";
}

或者保护你的代码......像这样

if (!this.IsPostBack)
{
   txtBox1.Text = "blah";
}
于 2009-12-29T16:56:45.563 回答
1

Page_Load 在回发期间被调用,它正在重置文本框中的值。改成

if (!this.IsPostBack)
        {
            txtBox1.Text = "blah";
            this._presenter.OnViewInitialized();

        }
于 2009-12-29T16:57:39.087 回答
0

就我个人而言,我会在视图中有一个属性来设置演示者的文本框值。在 OnViewInitialized() 或 OnViewLoaded() 中。

于 2009-12-29T19:28:30.063 回答