开发者

Textbox value doesn't get updated

开发者 https://www.devze.com 2023-01-22 14:18 出处:网络
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.

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 sende开发者_JAVA技巧r, 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.

e.g.

<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();
}
0

精彩评论

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