简体   繁体   中英

Using the ontextchange event to update the database

I am trying to use the onTextChange event to update the a textbox and then i am using a button to update it in the Database. Every time i try to use is it sets the value as "" i really dont know if i am doing something wrong or if i am doing it right. if someone could please help that would be great. thanks

Asp.net code:

<asp:TextBox ID="tbPLName" runat="server" OnTextChanged="tbPLName_TextChanged" />
<asp:Button ID="btUpdate" runat="server" Text="Update Name" OnClick="btUpdate_Click"   />

C# code:

string lastName;
protected void btUpdate_Click(object sender, EventArgs e)
    {
        tbCheck();
    }

    protected void tbPLName_TextChanged(object sender, EventArgs e)
    {
        lastName = tbPLName.Text;
    }

also if there is a better way of doing this please let me know. i really would like to do this the best way posible.

You are creating and assigning a variable that only exists in the TextChanged function. In the code you've shown you are assigning the input text to a variable, then closing the function (which gets rid of the variable).

EDIT: I should probably mention that you don't need the text_changed event if you are updating the database with the update button, that code would look like this:

protected void btUpdate_Click(object sender, EventArgs e)
{
    UpdateDatabase(tbPLName.Text);
}

EDIT2: You also say you want to update the database on TextChanged, but that's not what your code says (as little of it as there is).

If you DO want to do this, DON'T DO IT.

i have figured out what i am missing. i didn't need to use the ontextchanged event. i only needed to do:

if(!Page.IsPostBack)
{

    // code

}

at the top where the Page loaded was at and it worked fine. Then i could calling the method that would change the text in the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM