简体   繁体   中英

Textbox value doesn't get updated

I have a asp.net page with a datalist with a textbox and a button on it, on page load the textbox gets text in it, if I change the text and press the button the text doesn't get updated.

What am I doing wrong?

{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable table = CategoryAccess.GetProducts();

        ProductList.DataSource = table;
        ProductList.DataBind();
    }
}

protected void btn_Click(object sender, EventArgs e)
{
    string Name = textbox.Text;

    CategoryAccess.UpdateProducts(Name);
}
}

I had same problem. I found that I put textbox.text = "xxx" in Page_Load() but outside if(!ispostback) .

Try to add EnableViewState property in your textBox control and set the value to true .

eg

<asp:TextBox ID="textBox1"
             EnableViewState="true"
             MaxLength="25"
             runat="server"/>

or you can do it programatically:

protected void Page_Load(object sender, EventArgs e)
{
    textBox1.EnableViewState = true;
}

You need to bing again the new data...

protected void btn_Click(object sender, EventArgs e)
{
    string Name = textbox.Text;

    // you update with the new parametre
    CategoryAccess.UpdateProducts(Name);

    // you get the new data
    DataTable table = CategoryAccess.GetProducts();

    // and show it
    ProductList.DataSource = table;
    ProductList.DataBind();
}

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