[英]Textbox value doesn't get updated
我有一個asp.net頁面,上面有一個帶有文本框和按鈕的數據列表,在頁面加載時,文本框會在其中添加文本,如果我更改文本並按按鈕,則文本不會更新。
我究竟做錯了什么?
{
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);
}
}
我有同樣的問題。 我發現我將textbox.text = "xxx"
放在Page_Load()
但是放在if(!ispostback)
。
嘗試在您的textBox控件中添加EnableViewState屬性,並將其值設置為true
。
例如
<asp:TextBox ID="textBox1"
EnableViewState="true"
MaxLength="25"
runat="server"/>
或者您可以通過編程方式執行此操作:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.EnableViewState = true;
}
您需要再次獲取新數據...
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.