无法从 TextBox 获取用户输入

发布于 2024-08-16 06:46:50 字数 1654 浏览 4 评论 0原文

我开始对一些完全平庸的事情感到紧张:我没有从 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'm starting to loose my nerves on some completely banal thing: I don't get user input from a TextBox :S

I do something like this (code behind 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"
        }

And the 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>

I also tried to do this with DetailsView and bind it to a list but when I read the values in the edit mode i had the same problem.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2024-08-23 06:46:50

您在每个 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();

    }

You're setting the textbox Text property to "blah" on every Page_Load. Since ViewState has already been loaded at this point, you're overwriting whatever value the user entered.

If you only want to set the Text value one time, then make sure that you put it inside the if (!IsPostBack) check.

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

    }
假装不在乎 2024-08-23 06:46:50

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

Page_LoadButton1_Click 之前运行。

将代码从 Page_Load 移至此

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

或保护您的代码...就像这样

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

Your problem is that you are changing Value in Page_Load!

Page_Load is run before Button1_Click.

Move code from Page_Load to this

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

Or protect your code... like this

if (!this.IsPostBack)
{
   txtBox1.Text = "blah";
}
给我一枪 2024-08-23 06:46:50

Page_Load 在回发期间被调用,这会重置文本框中的值。更改为

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

        }

Page_Load is being called during the post back which is resetting the value in your text box. Change to

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

        }
兮子 2024-08-23 06:46:50

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

Personally i would have a property in the view to set the textbox value from the presenter. In OnViewInitialized() or OnViewLoaded().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文