开发者

Textbox in apsx.cs file won't update a changed text

I have a web application consisting of an aspx-file.

On page load two textboxes are filled with data (a "username" and a "password"). This works.

On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).

Why is that? And how can I tell my program to regard my changes?

My code is:

    protected void Page_Load(object sender, EventArgs e)
    {
        CredentialsManager cm = new 开发者_StackOverflowCredentialsManager();
        TextBox_Benutzername.Text = cm.Username;
        TextBox_Passwort.Text = cm.Password;
    }

    protected void Button_Speichern_Click(object sender, EventArgs e)
    {
        CredentialsManager cm = new CredentialsManager();
        cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
    }

EDIT:

It works with this improvement:

protected void Page_Load(object sender, EventArgs e)      
{          
    if(!IsPostBack) 
    { 
        CredentialsManager cm = new CredentialsManager();          
        TextBox_Benutzername.Text = cm.Username;          
        TextBox_Passwort.Text = cm.Password;      
    } 
}

For further information, see answers below. Thanks everyone!


Try checking for a postback -

private void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CredentialsManager cm = new CredentialsManager();
        TextBox_Benutzername.Text = cm.Username;
        TextBox_Passwort.Text = cm.Password;
    }
}

Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.


Try this,

protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack){
        CredentialsManager cm = new CredentialsManager();
        TextBox_Benutzername.Text = cm.Username;
        TextBox_Passwort.Text = cm.Password;
    }
}


You are assiging the value to the textboxes on every page load instead of firt page load.

Change the Page_Load method to :

protected void Page_Load(object sender, EventArgs e)     
{         
    if(!IsPostBack)
    {
        CredentialsManager cm = new CredentialsManager();         
        TextBox_Benutzername.Text = cm.Username;         
        TextBox_Passwort.Text = cm.Password;     
    }
} 


I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:

protected void Page_Load(object sender, EventArgs e)     
{
   if (!IsPostBack)
   {
     CredentialsManager cm = new CredentialsManager();
     TextBox_Benutzername.Text = cm.Username;
     TextBox_Passwort.Text = cm.Password;     
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜