Domanda

Sto iniziando a perdere i miei nervi su qualche cosa completamente banale: non ho ricevuto l'input dell'utente da una TextBox: S

faccio qualcosa di simile (codice dietro 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"
        }

E l'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>

Ho anche provato a fare questo con DetailsView e associarlo a un elenco, ma quando ho letto i valori nella modalità di modifica ho avuto lo stesso problema.

Tutte le idee?

È stato utile?

Soluzione

Si sta impostando la proprietà di testo Text a "bla" su ogni Load. Dal momento che ViewState è già stato caricato a questo punto, si sta sovrascrivendo qualsiasi valore immesso dall'utente.

Se si desidera solo impostare il valore di testo una sola volta, quindi assicurarsi che si inserisce all'interno del controllo if (!IsPostBack).

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

    }

Altri suggerimenti

Il problema è che si sta modificando Value in Page Load!

Page_Load viene eseguito prima Button1_Click.

Codice Sposta da Page_Load per questo

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

O proteggere il codice ... come questo

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

Page_Load viene chiamato durante la schiena dopo che si reimposta il valore nella casella di testo. Passare alla

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

        }

Personalmente mi sento di avere una proprietà nella vista per impostare il valore di testo dal presentatore. In OnViewInitialized () o OnViewLoaded ().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top