[英]textbox dynamically created not found
我使用此方法在表格單元格中插入文本框
protected void EditAttivitaClick(object sender, EventArgs e)
{
string attivitaID = ((ImageButton)sender).ID.Split('_')[2];
tableCell =(HtmlTableCell)FindControl("AttivitaDescrizione_" + attivitaID);
TextBox txt = new TextBox();
txt.Text = tableCell.InnerHtml;
txt.ID = "TxtAttivitaDescrizione_" + attivitaID;
tableCell.InnerHtml = "";
}
它可以正常工作。 此函數用於在db中保存文本框的值:
protected void SalvaAttivitaClick(object sender, EventArgs e)
{
string attivitaID = ((ImageButton)sender).ID.Split('_')[2];
TextBox txt = (TextBox)FindControl("TxtAttivitaDescrizione_" + attivitaID);
string a = txt.Text;
attivitaTableAdapter.UpdateID(txt.Text, Int32.Parse(attivitaID));
tableCell.Controls.Clear();
tableCell.InnerHtml = a;
}
但這是行不通的。 因為它找不到先前創建的文本框。
我也將EnableViewState =“ true”放在了文件aspx中。
為什么?
每次重新加載頁面時,都需要創建文本框,其中包括回發。
有關更多信息,請參見asp.net頁面生命周期 -您應該在Page.Init事件中創建動態控件,以便稍后使用。
如果您知道TextBox id,則可以從Form集合中檢索值,如果您只需要提交的值,則可以避免不必要地重新創建控件:
string attivitaID = ((ImageButton)sender).ID.Split('_')[2];
if(Request.Form["TxtAttivitaDescrizione_" + attivitaID] != null)
{
string a = Request.Form["TxtAttivitaDescrizione_" + attivitaID];
attivitaTableAdapter.UpdateID(a, Int32.Parse(attivitaID));
tableCell.Controls.Clear();
tableCell.InnerHtml = a;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.