[英]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. 我有一个asp.net页面,上面有一个带有文本框和按钮的数据列表,在页面加载时,文本框会在其中添加文本,如果我更改文本并按按钮,则文本不会更新。
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)
. 我发现我将
textbox.text = "xxx"
放在Page_Load()
但是放在if(!ispostback)
。
Try to add EnableViewState property in your textBox control and set the value to true
. 尝试在您的textBox控件中添加EnableViewState属性,并将其值设置为
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.