Question

I have an asp.net application in which i have this view :

<%@ Page Title="" Language="C#" MasterPageFile="~/Espace_Candidat/SousCandidat.master" AutoEventWireup="true" CodeBehind="InfoEdition.aspx.cs" Inherits="OummaWork.Espace_Candidat.InfoEdition" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent2" runat="server">
  <div class="divpere">
                <div class="divfille1">
                    <label>NOM (*)</label>
                </div>
                <div class="divfille2">
                    <asp:TextBox ID="nom"  Text ="First value" class="input-xlarge" runat="server" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="nom" Text="Champ obligatoire" ForeColor="Red"></asp:RequiredFieldValidator>
                </div>
            </div>
 <br />
            <br />
            <br />
            <div class="divpere">
                <div class="divfille1"></div>
                <div class="divfille2">
                    <asp:Button Style="color: #FFFFFF; background-color: #B81C22; margin-left: 73%" runat="server" ID="btn" Text="Modifier mes informations" OnClick="Valider" />
                </div>
            </div>
 </div>
</asp:Content>

In the code Behind :

 protected void Valider(object sender, System.EventArgs e)
        {
            string Nom = nom.Text;
       }

My problem that i got always as Nom's value "First value" even i change the text of the input!!

  • Why this happens?
  • How can i fix this problem?
Était-ce utile?

La solution

you initialize the value in page_load

99% you did not surround it in IsPostBack.

which means that the value is being re-initializd every time the page loads.

however, the page loads also on call backs after button submits.

you need to put this in page_load:

if(!IsPostBack)
{
    //initialize nom here;

}

this will make sure nom is only initialize when page first loads, and not every time you click a button

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top