views:

84

answers:

4

Hi,

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?

A: 

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";
}
Guilherme Ferreira
+3  A: 

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();

    }
womp
Aaaa thanks very much, I never thought about that Page_Load could fire before click event, well now I see.
Armen Ablak
A: 

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();

        }
stimms
A: 

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

magnus